Java NullPointerException error in Java (Eclipse)

AI Thread Summary
The discussion revolves around a Java code snippet that defines a `Cellphone` class and a `ModifyCellPhone` class, which initializes a 2D array of `Cellphone` objects. Participants address issues related to NullPointerExceptions, which occur when attempting to use an object that hasn't been initialized. A user initially encounters a NullPointerException while trying to copy elements from a null array and resolves the issue by using `System.out.print` to debug. The conversation highlights the importance of understanding NullPointerExceptions, including how to read stack traces to identify the source of errors. Suggestions are made for improving code readability, such as proper indentation and consistent use of braces, which help clarify the structure of loops and conditionals. Additionally, there is a discussion about the use of leading zeros in numeric literals, clarifying that `00000001` is equivalent to `1`. Overall, the thread emphasizes debugging techniques and coding best practices for handling null values effectively.
MarcL
Messages
170
Reaction score
2
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 :)
 
Technology news on Phys.org
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:

Java:
    String helloWorld = null;

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

but the statement:

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
 
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?
 
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.
 
MarcL said:
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.
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.

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:
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.
MarcL said:
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?
 
Last edited:
  • Like
Likes MarcL
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 :)
 
Back
Top