NullPointerException by accessing Object in JButton Array [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So I created a little snake game.
In my constructor I call the "CreateSnake" function, which creates a Snake with JButtons.
private void createSnake()
{
snakeButtonx[0] = 100; //Sets the starter position of the snake
snakeButtony[0] = 150;
JButton tempButton; //temporary Button for adding button to the Snake
// Initially the snake has length of 5
for (int i = 0; i < sizeSnake; i++)
{
snakeButton[i] = new JButton("" + i);
snakeButton[i].setEnabled(false); // Disable the buttons so you cant click it
tempButton = snakeButton[i];
Surface.addSnake(tempButton, i);
snakeButton[i].setBounds(snakeButtonx[i], snakeButtony[i], 10, 10);
snakeButtonx[i + 1] = snakeButtonx[i] - 10;
snakeButtony[i + 1] = snakeButtony[i];
}
}
Then I have a move function, which is called every 100 milliseconds via a thread
#SuppressWarnings("deprecation")
private void move()
{
snakeButtonx[0] += directionx; //Set the position of the head of the snake
snakeButtony[0] += directiony;
for (int i = 0; i < sizeSnake; i++)
{
snakeButtonPos[i] = snakeButton[i].getLocation(); //Fills the snakeButtonPos integer with the positions of every Button
}
snakeButton[0].setBounds(snakeButtonx[0], snakeButtony[0], 10, 10);
for (int i = 1; i < sizeSnake; i++)
{
snakeButton[i].setLocation(snakeButtonPos[i - 1]);
}
show();
}
So. Till here, it works fine. But whenever I want to add a sixth JButton to my JButton array, I get a nullpointer exception in my move methode.
private void Grow()
{
JButton tempButton = new JButton();
int newIndex = sizeSnake + 1;
// Add the new Button to the Button Array
snakeButton[newIndex] = new JButton();
snakeButton[newIndex].setEnabled(false);
tempButton = snakeButton[newIndex];
Surface.addSnake(tempButton, newIndex);
// Position is irrelevant, cause the position will be fixed at the next call of move()
snakeButton[newIndex].setBounds(200, 300, 10, 10);
sizeSnake = sizeSnake + 1;
}
The sixth JButton is in the Jbutton array while Im in my Grow() function. But by the next call of move(), there isnt a JButton with the index 6 anymore.
Why do I get a NullPointerException? Where is the mistake?
EDIT: The error occurs on this line after the sixth loop in my move() function: snakeButtonPos[i] = snakeButton[i].getLocation();
And the exact exception:
Exception in thread "Thread-2" java.lang.NullPointerException
at Snake.move(Snake.java:92)
at Snake.run(Snake.java:217)
at java.lang.Thread.run(Unknown Source)
EDIT2: I call move and Grow here in my Thread
#Override
public void run() {
while (true)
{
CheckPosition(); //CheckPosition includes Grow() if the snake intersects with an object
move();
try
{
Thread.sleep(100);
}
catch (InterruptedException ie)
{
System.out.println(ie);
}
}
}

In Grow(), try changing int newIndex = sizeSnake + 1; to int newIndex = sizeSnake;. Since your for loops all go until i < sizeSnake, that means the last JButton is actually the one at snakeButton[sizeSnake - 1], not the one at snakeButton[sizeSnake].

Since some code is missing, a shot in the dark:
private void Grow()
{
JButton tempButton = new JButton();
int newIndex = sizeSnake + 1;
// Add the new Button to the Button Array
snakeButton[newIndex] = new JButton();
snakeButton[newIndex].setEnabled(false);
...
}
Say sizeSnake is 5, then newIndex is set to 6, so your snakeButton array looks like this afterwards:
[button, button, button, button, button, null, button, null, ...]
Because index 6 is the seventh element. The newIndex is unnecessary.
The move() method should be
private void Grow()
{
JButton tempButton = new JButton();
// Add the new Button to the Button Array
snakeButton[sizeSnake] = new JButton();
snakeButton[sizeSnake].setEnabled(false);
tempButton = snakeButton[sizeSnake];
Surface.addSnake(tempButton, sizeSnake);
// Position is irrelevant, cause the position will be fixed at the next call of move()
snakeButton[sizeSnake].setBounds(200, 300, 10, 10);
sizeSnake = sizeSnake + 1;
}

Related

For Loop causing a Window to pop up more times than expected

I'm attempting to build Minesweeper in Java (Using Eclipse, if that matters for any reason whatsoever) and while making it, I've encountered an issue I haven't had before.
The window that's supposed to show up whenever a mine is pressed is opened... A lot. The window is only meant to open once and prompt the user to either continue or give up.
Important to note this only occurs whenever a mine is pressed on the top row of buttons. Not entirely sure if this is important, but the amount of times a window pops-up is 11. There are also no error codes given.
The following chunk of code is for whenever a user clicks on a mine.
//These are here just for clarification
private int continues = 3;
private Listen l = new Listen();
private class Listen extends MouseAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (source == mines[i][j])
{
//Though probably not optimal, I created mines based around a number referred to as difficulty
if (minePlacement[i][j] <= difficulty)
{
Frame f = new Frame();
Label L = new Label("You have " + continues + " continues remaining.");
Button B = new Button ("Try Again?");
Label La = new Label("You're out of continues...");
Button But = new Button("Exit");
f.setLayout(new FlowLayout());
B.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
f.dispose();
}
});
if (continues <= 0)
{
f.add(La);
f.add(But);
/*
For clarification,
This actionListener is used
solely to do System.exit(0);
*/
But.addActionListener(li);
f.setSize(250,125);
}
else
{
f.add(L);
f.add(B);
f.setSize(250,100);
}
f.setLocationRelativeTo(null);
f.setAlwaysOnTop(true);
f.setVisible(true);
f.setTitle("You Hit A Mine!");
continues--;
}
This following chunk of code is used in order to build the board itself.
for (int i = 0; i < rows; i++)
{
int x = (int) ((Math.random() + .1) * 10);
minePlacement[i][0] = x;
mines[i][0] = new Button();
mines[i][0].addMouseListener(l);
mines[i][0].addActionListener(l);
add(mines[i][0]);
for (int j = 0; j < cols; j++)
{
int y = (int) ((Math.random() + .1) * 10);
minePlacement[i][j] = y;
mines[i][j] = new Button();
mines[0][j].addMouseListener(l);
mines[0][j].addActionListener(l);
mines[i][j].addMouseListener(l);
mines[i][j].addActionListener(l);
add(mines[i][j]);
}
I've tried looking for other posts which may answer my question, though as it stands, I have failed to find any other posts that have been coded in Java. I've mainly found them in C++ or C, to which I don't understand how the solution was brought about.
I'd like to know if there's any way to set a limit on the amount of windows on the screen, or if there's a way to prevent the window from popping up more times than expected.
Thanks in advance!

While buttons are changing color every second, how do I make the button I press stop while the other buttons continue switching colors?

I am creating a grid of 4 by 2 buttons and I want them to continuously change color. A button will stop changing colors once I press it. I have having trouble figuring how to make that one button stop changing colors if I press on it and have other buttons to continuously change color. If I press another button, that button stops changing color as well.
public class Hw1 extends JPanel {
static JFrame jf;
static JPanel jp;
static JButton jb;
public static void main(String[] args) {
jf = new JFrame("Hello World!");
int xAxis = 500;
int yAxis = 300;
jf.setSize(xAxis, yAxis);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new JPanel();
int num = 8;
JButton[] buttonList = new JButton[num];
int count = 0;
// creates buttons and initially assigns random color
for(int i = 0; i < 2 ; i++){
for(int j = 0; j < 4; j++){
jb = new JButton("press me!");
jb.setBounds(i * (xAxis/2), j * (yAxis/4), xAxis/2, yAxis/4);
jb.setVisible(true);
jb.setOpaque(true);
int primeR = (int)(Math.random() * 255+ 0);
int primeG = (int)(Math.random() * 255 + 0);
int primeB = (int)(Math.random() * 255 + 0);
Color random = new Color(primeR, primeG, primeB);
jb.setBackground(random);
// adds action listener to each button aka checks if button is pressed
jb.addActionListener(new ActionListener(){
// action if button is pressed
//if pressed change stop changing colors
public void actionPerformed(ActionEvent ae) {
JButton theButton = (JButton)ae.getSource();
theButton.setBackground(random);
}
});
buttonList[count] = jb;
count++;
jp.add(jb);
}
}
jf.add(jp);
jf.setVisible(true);
for (int k = 0; k < buttonList.length; k++) {
new Thread(){
public void run(){
while(true){
try {
sleep(1000);
}
catch (InterruptedException ex) {
}
for (int i = 0; i < buttonList.length; i++) {
int primeR = (int)(Math.random() * 255);
int primeG = (int)(Math.random() * 255);
int primeB = (int)(Math.random() * 255);
Color random = new Color(primeR, primeG, primeB);
buttonList[i].setBackground(random);
}
}
}
}.start();
}
}
}
you need to keep state of button.
If you have grid of 4 button, make an array of it's state buttonState = new Boolean[2][2]
when button on position x,y is pressed, set value of [x][y] to true. check buttonState before changing color of button.
OR
Overwrite JButton and add bolean field pressed to it, and set it to true in onClick, and based on that value change or not change color

How to get Specific Array List Item

I have a little issue with selecting from an array list. I am writing some code to enable me fix about 10 JButtons in a circle, I got that right, but then ..... I want to set an actionListener on each of the Buttons, but I don't get it, all the buttons inherit the actions required for one. How do I make it specific,... here's my code.... Thanks in advance!
private JButton quest;
public Beginner() {
int n = 10; // no of JButtons
int radius = 200;
Point center = new Point(250, 250);
double angle = Math.toRadians(360 / n);
List<Point> points = new ArrayList<Point>();
points.add(center);
for (int i = 0; i < n; i++) {
double theta = i * angle;
int dx = (int) (radius * Math.sin(theta));
int dy = (int) (radius * Math.cos(theta));
Point p = new Point(center.x + dx, center.y + dy);
points.add(p);
}
draw(points);
}
public void draw(List<Point> points) {
JPanel panels = new JPanel();
SpringLayout spring = new SpringLayout();
// Layout used
int count = 1;
for (Point point : points) {
quest = new JButton("Question " + count);
quest.setForeground(Color.BLUE);
Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
quest.setFont(fonte);
add(quest);
count++;
spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels);
spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels);
setLayout(spring);
panels.setOpaque(false);
panels.setVisible(true);
panels.setLocation(10, 10);
add(panels);
// action Listener to be set on individual buttons
quest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if (quest.equals(points.get(5)))
;
String c = "Hello!";
JOptionPane.showMessageDialog(null, c);
}
});
}
}
The problem is that expression
if (quest.equals(points.get(5)));
does nothing. I guess it should be rewritten like this
if (quest.equals(points.get(5))) {
String c = "Hello!";
JOptionPane.showMessageDialog(null, c);
}
The way I am understanding the question is that you have multiple buttons and you would like each button to have it's own action associated with it. There are a couple ways to go about doing this. Either you create a new ActionListener for each JButton depending which button you are creating.
You can also create a large case/switch or if/else within the ActionListener that gets determined by which button was selected. To do this you can call the getActionCommand() function for the ActionEvent object.
quest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand().equals("Question 1"))
{
String c = "Hello!";
JOptionPane.showMessageDialog(null, c);
}
else if(a.getActionCommand().equals("Question 2"))
{
//have it do something else
}
//and so on so forth
}
});
You need to rethink the entire design. You have this line:
if (quest.equals(points.get(5))) {
But points is a list containing Point objects; points.get(5) returns a Point.
quest is a JButton. How can a JButton instance equal a Point instance?

JFrame not showing up after running

I am working a JFrame calculator, I am not to familiar with so I was following a tutorial, and I've double checked it a couple times.
It's not working, its not throwing any errors or anything, it will compile and run. Just nothing pops up.
I am using Eclipse, and it is giving two warnings,
one about my constructor not being used in my main
that my class says this: The serializable class Gui does not declare a static final serialVersionUID field of type long.
I'm not sure why those would come up, but my code seems to be like the tutorial.
Thank you in advanced.
Ps. excuse the all commons, I just making sure I can looking back at it without having to refer back to the tutorial.
// This will make the visuals of the calc
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements ActionListener {
JPanel[] row = new JPanel[5];
JButton[] button = new JButton[19];
String[] buttonString = {"7", "8", "9", "+", // This is a string for the buttons that we will later apply to
"4", "5", "6", "-", // the the buttons with a loop, instead of 19 lines for each one
"1", "2", "3", "*",
".", "/", "C", "√",
"-/+", "=", "0"};
int[] dimW = {300, 45, 100, 90}; // An Array for the different widths of the display and buttons
int[] dimH = {35, 40}; // An array for the different heights of the display and buttons
Dimension displayDimension = new Dimension(dimW[0], dimH[0]); // The dims for the display using the first ints of the arrays
Dimension regularDimension = new Dimension(dimW[1], dimH[1]); // The
Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
Boolean[] function = new Boolean[4]; // A boolean array to tell, which operator we are using
double[] temp = {0, 0}; // A temp array for the calc, might not use when using my stack calc
JTextArea display = new JTextArea(1, 20); // This is the display where the text will be displayed
Font font = new Font("Ariel", Font.BOLD, 14);
Gui() {
super("Gui");
setDesign();
setSize(380, 250); // Set the frame size
setResizable(false); // Makes so it can't be resized, can mess up layout if it true
setDefaultCloseOperation(EXIT_ON_CLOSE); // What happens when it closes
GridLayout grid = new GridLayout(5, 5); // Since we need a grid of 5 by 5 for the buttons this makes the grid
setLayout(grid);
for (int i = 0; i < 4; i++) // Sets values for the function array, might use might not with my clac
{
function[i] = false;
}
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER); // This will only be use to layout row 1
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1); // The ints are used to give a 1 pt gap vert and horiztal
for (int i = 0; i < 5; i++) // Intinalizing the Jpanel row's so we can use them
{
row[i] = new JPanel();
}
row[0].setLayout(f1); // Since we need the first row to have the special layout of f1 we just assign it
for (int i = 1; i < 5; i++) // Since we already set the first row "Row[0]" to f1, we have to start with row 2 or row[1] for i
{
row[i].setLayout(f2);
}
// After this all the rows have the correct layout, we can set up the buttons
// And set the same thing to each button with a loop
for (int i = 0; i < 19; i++) {
button[i] = new JButton(); // Creates a new button for each button in the array
button[i].setText(buttonString[1]); // Sets text on the button with the text from the list, in ButtonString
button[i].setFont(font); // Makes it nice looking with the fancy font we used in the font line
button[i].addActionListener(this); // This is what makes the button actually work
}
// Buttons done we can move to the display set up
display.setFont(font); // Set the fancy font to the display too
display.setEditable(false); // Makes it so no input from the keyboard, will have to change this on final product
display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); // Makes it pop in right to left
// With the fonts and everything intinlized we can start to set the sizes for the compents
display.setPreferredSize(displayDimension); // Sets the size of the display
// We can use a loop for the regular buttons, i think all but zero
for (int i = 0; i < 14; i++) {
button[i].setPreferredSize(regularDimension); // Sets the size of the regular buttons
}
for (int i = 14; i < 18; i++) {
button[i].setPreferredSize(rColumnDimension); // Sets the size of the right column of buttons, the operrantors
}
button[18].setPreferredSize(zeroButDimension); // Sets the size of the zero button since its bigger
// Now that we got evrything sized up time to add everything to the panel
row[0].add(display); // Adds the display to row 1
add(row[0]); // Adds row 1 to the panel
for (int i = 0; i < 4; i++) {
row[1].add(button[i]); // all the number buttons to the row
}
row[1].add(button[14]); // adds the operator button to the row
add(row[1]); // adds the row
for (int i = 4; i < 8; i++) {
row[2].add(button[i]); // all the number buttons to the row
}
row[2].add(button[15]); // adds the operator button to the row
add(row[2]); // adds the row
for (int i = 8; i < 12; i++) {
row[3].add(button[i]); // all the number buttons to the row
}
row[3].add(button[16]); // adds the operator button to the row
add(row[3]); // adds the row
row[4].add(button[18]);
for (int i = 12; i < 14; i++) {
row[4].add(button[i]); // all the number buttons to the row
}
row[4].add(button[17]); // adds the operator button to the row
add(row[4]); // adds the row
setVisible(true); // Makes so you can see it
}
// Not sure what this is
public final void setDesign() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
}
// Will be use to make actionlistener work
public void actionPerformed(ActionEvent ae) {
}
public void main(String[] arguments) {
Gui c = new Gui();
}
}
You should really have this in your main method
public static void main(String[] args){
Gui c = new Gui();
c.setVisible(true);
}

Need assistance with frogger type game

I'm in the process of creating a frogger type game and have gotten pretty far in getting the program to do what I want it to do. However, I'm starting to think that to finish the game I will have to use way to much code and there must be a simpler way achieve the same results. I'm not looking for an answer, just need some more information.
Question 1: What can I use for the images that represent the moving Icons or cars? I'm currently using JButtons. The problem is that is difficult to get the buttons to move uniformly and I want to use 24 different moving Icons and from what I've learned so far I will have to add a new JButton for each icon.
Question 2: The way that I've gotten the Jbutton icons to move is to use a timer delay and then a counter to increment the x values. This works for the most part, but is there a better, perhaps simpler, way to move icons across the screen?
Any tips, tutorials etc are greatly appreciated.
Here is one of the classes that I've created to get movement of the icons:
public class EnemyJPanel extends JButton {
JButton enem = new JButton();
JButton enem12 = new JButton();
JButton enem13 = new JButton();
JButton enem1 = new JButton("1");
JButton enem2 = new JButton("2");
JButton enem3 = new JButton("3");
JButton enem4 = new JButton("4");
JButton score = new JButton("Score");
JButton enem5 = new JButton("5");
JButton enem6 = new JButton("6");
JButton enem7 = new JButton("7");
JButton enem8 = new JButton("8");
JButton yard = new JButton("50 Yard Line");
int i = 16;
int u = 576;
int d = 16;
int n = 576;
int k = 16;
int l = 16;
int dummyval = 16;
public EnemyJPanel(){
super();
setLayout(null);
enem1.setBounds(16,300,40,55);
enem2.setBounds(16,245,40,55);
enem3.setBounds(16,190,40,55);
enem4.setBounds(16,135,40,55);
score.setBounds(16,80,601,55);
yard.setBounds(16,355,601,55);
enem5.setBounds(16,410,40,55);
enem6.setBounds(16,465,40,55);
enem7.setBounds(16,520,40,55);
enem8.setBounds(16,575,40,55);
enem12.setBounds(16,300,40,55);
enem13.setBounds(16,300,40,55);
add(enem1);
add(enem2);
add(enem3);
add(enem4);
add(score);
}
public void addEnemy(){
enem1.setBounds(16,300,40,55);
enem2.setBounds(16,245,40,55);
enem3.setBounds(16,190,40,55);
enem4.setBounds(16,135,40,55);
score.setBounds(16,80,601,55);
add(enem1);
add(enem2);
add(enem3);
add(enem4);
add(score);
}
public void enemyMovement(){
i++;u--;d++;n--; // increments lateral movement from a timer in
dummyval++; // the dummy value is needed to keep the icons looping
dummyval = dummyval + 2;
enem1.setBounds(i,300,40,55);
i = i + 2;
if (dummyval > 176){
k++; k = k + 2;
enem12.setBounds(k,300,40,55);
}
if (k > 176){
l++;
l = l + 2;
enem13.setBounds(l,300,40,55);
}
enem2.setBounds(u,245,40,55);
enem3.setBounds(d,190,40,55);
enem4.setBounds(n,135,40,55);
enem5.setBounds(i,410,40,55);
enem6.setBounds(u,465,40,55);
enem7.setBounds(d,520,40,55);
enem8.setBounds(n,575,40,55);
if(i > 576){ // resets button
i = 16;
}
if(k > 576){
k = 16;
}
if(u < 16){
u = 576;
}
u = u - 2; // increase lateral speed
if(d == 576) {
d = 16;
}
if(n < 16){
n = 576;
}
n = n - 5; //increases lateral speed
}
}
The problem is created because you try to manage all the "stuff" separately. It looks like you may be missing some basic information on classes
First, I would create a custom class, something like
class ButtonObject extends JButton
{
public ButtonObject(String text, int x, int y, int width, int height)
{
super(text);
this.setBounds(x, y, width, height);
}
}
You also may want to take a look at arrays and create an array of your new ButtonObject.
The for loop will help you get through all the objects in your array.
ButtonObject[] enemies = new ButtonObject[10];
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
int y = 300 - (i * 55);
enemies[i] = new ButtonObject(text, 16, y, 40, 55);
}
There is probably a better way to do it than buttons but you may want to stick with them for now for simplicity.

Categories