Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Re: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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

    /* -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


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Please do not hijack someone else's post.

    You created an array...
    Control robot[]=new Control [k];
    ...but have not instantiated the elements of the array.
    robot[index] = new Control();

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    even with that it's not working.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Exception in thread "main" java.lang.NullPointerException
            at hw.Main.main(Main.java:20)
    Java Result: 1

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Mr. copeg..
    are you still there?
    PLZ Help me.

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    waiting for help :'(

  8. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 04:36 PM
  4. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM