Re: Exception in thread "main" java.lang.NullPointerException
PLZ I need a help
I have an error while running the Prog.
-------
Exception in thread "main" java.lang.NullPointerException
at Robot.main(Robot.java:20)
-------
this is my code
this is control class.
Code :
public class Control {
private int x;
private int y;
public Control(){
this.x=0;
this.y=0;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
-------------------------
this is main class
Code :
/* -The program is designed to control the movement of a robot in the chemical room.
Robot must not hit any room wall (10*10).
* 2 robots not allowed to be located in the same place.
You should expect the input errors entered by the user.
*/
import java.util.*;
import javax.swing.*;
public class Robot {
public static void main(String[] args) {
int i;
int k =Integer.parseInt(JOptionPane.showInputDialog(null,
"How many Robot you want to play with?"));
Control robot[]=new Control [k];
for ( i=0; i<robot.length; i++){
int x = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter The X coordinate of the robot:"));
int y = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter The y coordinate of the robot:"));
robot[i].setX(x);
robot[i].setY(y);
System.out.println(robot[i].getX()+" "+robot[i].getY());
{
if(robot[i].getX()>10 || robot[i].getY()>10){
robot[i].setX(0);
robot[i].setY(0);
JOptionPane.showMessageDialog(null, "Hitting wall.. \n Robot coordinate is: " +
"("+robot[i].getX()+","+robot[i].getY()+")");
}
else
JOptionPane.showMessageDialog(null,"Robot corrdinate is:\n"+"("+robot[i].getX()+
","+robot[i].getY()+")");
}
}
}
}
PLZ Help me :(
Re: Exception in thread "main" java.lang.NullPointerException
Please do not hijack someone else's post.
You created an array...
Quote:
Control robot[]=new Control [k];
...but have not instantiated the elements of the array.
Code :
robot[index] = new Control();
Re: Exception in thread "main" java.lang.NullPointerException
even with that it's not working.
Re: Exception in thread "main" java.lang.NullPointerException
What's not working...please post any error messages in their entirety and any updated code you may have.
Re: Exception in thread "main" java.lang.NullPointerException
after i enter the count of robot EX: 2
it ask me for coordinates: EX: 6 -for X- and 5 -for Y-.
it display this message
Code :
Exception in thread "main" java.lang.NullPointerException
at hw.Main.main(Main.java:20)
Java Result: 1
Re: Exception in thread "main" java.lang.NullPointerException
Mr. copeg..
are you still there?
PLZ Help me.
Re: Exception in thread "main" java.lang.NullPointerException
Re: Exception in thread "main" java.lang.NullPointerException
Do not continue to bump threads like this - despite what you think it could hurt your chances of receiving help. We aren't a 24hr service waiting at the edge or our seat for the next problem to come along, just a bunch of volunteers. Further, you did not follow my advice and post your updated code. That error message tell you exactly where the exception is being thrown - line 20. Go to your code and look at line 20. Add some System.out.println statements to see what is null, and work backwards to try and figure out if you ever instantiate the object.