NullPointerException error in Java (Eclipse)

In summary, the conversation revolved around a code for a Cellphone class and its modifications. The main issue was a NullPointer exception, which could be solved by using a try-catch block or handling the problem of uninitialized objects. The conversation also discussed the use of brackets and proper indentation in coding.
  • #1
MarcL
170
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
  • #2
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
 
  • #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?
 
  • #4
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.
 
  • #5
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
  • #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 :)
 

Related to NullPointerException error in Java (Eclipse)

1. What is a NullPointerException error in Java (Eclipse)?

A NullPointerException error in Java (Eclipse) is a common runtime error that occurs when a program tries to access or use an object that has not been initialized or is set to null. This error typically occurs when a program attempts to call a method or access a variable on an object that does not exist.

2. What causes a NullPointerException error in Java (Eclipse)?

A NullPointerException error in Java (Eclipse) is typically caused by a programming mistake, such as trying to use an object without first initializing it or passing a null value to a method that does not accept it. It can also be caused by referencing an object that has been deleted or no longer exists in the program's memory.

3. How do I fix a NullPointerException error in Java (Eclipse)?

To fix a NullPointerException error in Java (Eclipse), you will need to locate the source of the error by analyzing your code and identifying where an object is being used without first being initialized. Once you have identified the problem, you can either initialize the object properly or add code to handle null values and prevent the error from occurring.

4. How can I prevent a NullPointerException error in Java (Eclipse)?

To prevent a NullPointerException error in Java (Eclipse), it is important to always initialize objects before using them and to check for null values before attempting to use them in your code. It is also a good practice to handle potential null values in your code, either by throwing an exception or using conditional statements to handle them appropriately.

5. Can a NullPointerException error be ignored in Java (Eclipse)?

No, a NullPointerException error cannot be ignored in Java (Eclipse). Ignoring the error can lead to unexpected program behavior or even crashes. It is important to properly handle these errors by fixing the underlying issue or handling null values in your code.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
789
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top