Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

NullPointerException error in Java (Eclipse)

  1. Apr 15, 2015 #1
    Code (Java):
         class Cellphone {
     
           private String brand;
            private long serialNumber;
            private double Price;
       
            public Cellphone (String br, long sN, double Pr)
        {
            brand= br;
            serialNumber = sN;
            Price = Pr;
        }
    public Cellphone(Cellphone aCellphone)
       {
           this(aCellphone.getbrand(), aCellphone.getserialNumber(), aCellphone.getPrice());
           }
    }

    public class ModifyCellPhone {
        public static void main (String[] args)
        {  
            Cellphone[][] cellphoneArr  = new Cellphone[10] [10];
            for (int i=0; i < cellphoneArr.length-1; i++)
              { for (int m=0; m < cellphoneArr[i].length; m++)
                 {  if (i % 3 == 0)
                   cellphoneArr[i][m]= new Cellphone("Samsung", 00000001 + (2 * i+1), 500.4 + i);
                 else if (i % 3 == 1)
                     cellphoneArr[i][m] = new Cellphone("LG", 0000001 * (2 * i), 500.6 + i);
                 }
            for (int y=9; y<cellphoneArr.length; y++)
               { for (int n=0; n<cellphoneArr[y].length; n++)
                    {              
                      cellphoneArr[y][n] = new Cellphone(cellphoneArr [7][n]);
                 
                     }
                 
                }

            for (int p = 0; p < cellphoneArr.length; p++) {

                   // Loop and display sub-arrays.
                   for (int x = 0; x < cellphoneArr[p].length; x++) {
                   System.out.print(cellphoneArr[p][x] + " ");
                   }
                   System.out.println();
              }


            }
           }
    }
     
    Thanks for the help :)
     
  2. jcsd
  3. Apr 15, 2015 #2

    jedishrfu

    Staff: Mentor

    We can't help you without the NullPointer stack trace. The trace would show the line number of the offending statement.

    If you look at the objects used in the statement null pointer exceptions arise from trying to use an object instance that hasn't been initialized

    As an example:

    Code (Java):

        String helloWorld = null;

        printf("The string value is: "+helloworld);      // will simply print: The string value: null
     
    but the statement:

    Code (Java):

       printf("The string value is:"+helloWorld.toString());  // will cause a null pointer exception
     
    will cause a null pointer exception because you're trying to call the toString() method on helloWorld but helloWorld is defined as null
     
  4. Apr 15, 2015 #3
    Would you mind telling me telling me what is the NullPointer stack trace?

    Also I figured it out using system.out.print. Realized the array I was trying to copy was null, so I was initializing a null by a null, because modulus of 3 gives a remainder of 0,1, or 2. Silly me.

    However, I would like to know what the NullPointer is for. I was never told to use it in class ( or how as a matter of fact). Is it useful to detect nullpointer errors?
     
  5. Apr 16, 2015 #4

    jedishrfu

    Staff: Mentor

    Good programmers realize that sometimes things don't get initialized correctly. In those cases you might get NullPointer exceptions and other kinds of errors.mTo handle this problem we would bracket the code with a try catch block and catch the null pointer or cleanup things like close files and just exit the program when it happened.
     
  6. Apr 16, 2015 #5

    Mark44

    Staff: Mentor

    Presumably in the code below, although I don't see what mod 3 has to do with your problem. Trying to access an object before it has been initialized is probably the source of your exception.

    Code (Java):
    for (int i=0; i < cellphoneArr.length-1; i++)
              { for (int m=0; m < cellphoneArr[i].length; m++)
                 {  if (i % 3 == 0)
                   cellphoneArr[i][m]= new Cellphone("Samsung", 00000001 + (2 * i+1), 500.4 + i);
                 else if (i % 3 == 1)
                     cellphoneArr[i][m] = new Cellphone("LG", 0000001 * (2 * i), 500.6 + i);
                 }
    I have to say, though, that although your indentation aids in understanding your code flow, your use of braces is pretty random. I would format the code above like this:
    Code (Java):
    for (int i=0; i < cellphoneArr.length-1; i++)
    {
       for (int m=0; m < cellphoneArr[i].length; m++)
       {
          if (i % 3 == 0)
                cellphoneArr[i][m]= new Cellphone("Samsung", 00000001 + (2 * i+1), 500.4 + i);
          else if (i % 3 == 1)
                cellphoneArr[i][m] = new Cellphone("LG", 0000001 * (2 * i), 500.6 + i);
        }
        .
        .
        .
    }
    Doing it like this makes if very obvious exactly where the bodies of for loops, etc. begin and end. I also use braces on if blocks, even if their bodies are only one line. It's too easy to come back and add a line in one of these single-line bodies, and forget that you now need a pair of braces.

    Also, what is the point of writing 00000001? For one thing, this is just the literal 1. For another, this is octal (base-8) 1, which happens to be the same as decimal 1.
     
    Last edited: Apr 16, 2015
  7. Apr 16, 2015 #6

    jedishrfu

    Staff: Mentor

    The trace stack follows the NullPointer exception message unless your code suppressed it. It would say NP occurred at xxx.xxx.xxx (xxx.java @999)

    The @999 tells you line number where it occurred and the xxx.java tells you the class file

    http://frequal.com/java/DebuggingNullPointerExceptions.html
     
  8. Apr 16, 2015 #7
    Well yeah, I'm trying to my best to follow my own "convention" because I just started coding ( engineering in college). However your use of bracket does help more the understanding of the code. As for the 0000001, I was just thinking of a long number, I changed it to 11111111. Thanks both of you for your help :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: NullPointerException error in Java (Eclipse)
Loading...