NullPointerException error in Java (Eclipse)

  • Context: Java 
  • Thread starter Thread starter MarcL
  • Start date Start date
  • Tags Tags
    Eclipse Error Java
Click For Summary

Discussion Overview

The discussion revolves around a NullPointerException error encountered in a Java program using Eclipse. Participants explore the causes of the error, share debugging strategies, and discuss the significance of the NullPointer stack trace. The scope includes programming concepts, error handling, and code formatting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant notes that a NullPointerException arises from trying to use an object instance that hasn't been initialized, providing an example of how this occurs.
  • Another participant expresses confusion about the NullPointer stack trace and its utility in detecting null pointer errors, indicating they were not taught about it in class.
  • A suggestion is made that good programming practices involve using try-catch blocks to handle potential NullPointerExceptions and other errors.
  • Concerns are raised about the formatting of the provided code, particularly regarding indentation and the use of braces, which some participants argue could improve code readability.
  • Discussion includes a critique of using leading zeros in numeric literals, with one participant explaining that it can be interpreted as octal in Java.
  • Another participant clarifies that the stack trace follows the NullPointer exception message and provides a format for understanding it, including line numbers and class file references.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the NullPointerException and its stack trace. While some agree on the importance of error handling and code formatting, others remain uncertain about specific aspects of the NullPointerException and its implications.

Contextual Notes

There are unresolved questions about the proper use of the NullPointer stack trace and its significance in debugging, as well as differing opinions on code formatting practices.

Who May Find This Useful

This discussion may be useful for beginner programmers, particularly those learning Java and dealing with error handling and debugging techniques.

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   Reactions: 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 :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K