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 3 of 3

Thread: NullPointerException @@

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException @@

    package assignment;
    import java.util.*;
    import java.util.Stack;
     
    public class Assignment {
     
       public static void main(String[] args) {
            // TODO code application logic here
            boolean exit = false;
            boolean flag = false;
            MyArrayList<GenericStack[][]>region = new MyArrayList<>();
            Container container = new Container();
            GenericStack<Container>[][]storage=new GenericStack[10][10];
     
         while(exit == false){
            System.out.println("Please select the function that you want to preform");
            System.out.println("1) Load Container into region");
            System.out.println("2) Unload the container from the region");
            Scanner input = new Scanner(System.in);
            int selection = input.nextInt();
            //switch case to select option
            switch(selection){
                case 1 : LoadObject( storage, container);
                    break;
                default: System.out.println("Please key in the correct input :");
     
            }//end of the switch case
            //option for exit
            while(flag==true)
            System.out.println("Do you want to exit ? (Y/N))");
            Scanner inputExit = new Scanner(System.in);
            if(inputExit.nextInt()=='Y' || inputExit.nextInt()== 'y')
                exit = true;
            else 
                if( inputExit.nextInt()=='N' || inputExit.nextInt()=='n')
                    exit = false;
                else {
                   //If the user key in a non-valid input
                    System.out.print("Please key in the correct input.");
                    flag =true;
                }
            } 
        }//end of thw while loop
     
        public static void LoadObject(GenericStack[][]storage, Container container){
     
            System.out.println("How many container");
            Scanner input = new Scanner(System.in);
            int counter = input.nextInt();
            int i, j;
     
            for(i = 0; i <10 ; i++){
                for(j = 0 ; j< 10 ; j++){
                    if(counter>0){
                        if(!storage[i][j].isEmpty()){             //<--------
                            for(i = storage[i][j].getSize() ; i < 10 ; i++ ){
                                System.out.println("Please key in the container id :\n");
                                int objectId = input.nextInt();
                                container.SetObjectId(objectId);
                                System.out.println("Please key in the container description :\n");
                                String objectDesc = input.nextLine();
                                container.SetObjectDesc(objectDesc);
                                storage[i][j].push(container);
                                counter--;
                                 }//end of the insertion
                            }
                         }//end of the number of container
                       else
                            i = 10; j =10;//end the check empty if is finish container
                        }//end of the for loop
                    }//end of the for loop
                }
     
     
         }



    Here is part of my coding @@
    i have no idea why there is a "NullPointerException" error when i run it. I have no idea what it is please help me.
    Last edited by jps; August 21st, 2013 at 05:54 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: NullPointerException @@

    Please post your code in code tags.

    Why the '@@'? Is that relevant?

    If you're getting errors you want help with, post the exact error, the entire error and stack trace, copied and pasted from just as it appears at your end. If possible, post a short, runnable code example that demonstrates the error.

    I believe your problem is because arrays are created with null elements until otherwise initialized. The storage[][] array is full of nulls.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: NullPointerException @@

    Quote Originally Posted by GregBrannon View Post
    Why the '@@'? Is that relevant?
    Some sort of annoying emoticon perhaps.
    Improving the world one idiot at a time!

Similar Threads

  1. NullPointerException
    By Alket in forum Member Introductions
    Replies: 1
    Last Post: June 7th, 2012, 07:09 AM
  2. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM