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: can someone help me understand why i am getting this error.

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy can someone help me understand why i am getting this error.

    This is a program to assign global variable with command line args:

    class B
    {
    int x;
    B ref;
    B (int data)
    {
    this.x=data;
    }
    }

    public class MyClass {
    public static void main(String args[]) {
    B b1 = null;
    for (String s1 : args)
    {
    if (b1 == null)
    {
    b1 = new B(Integer.parseInt(s1));
    }
    else
    {
    B element = b1;
    while (element != null)
    {
    element=element.ref;
    }
    element.ref=new B(Integer.parseInt(s1));
    }
    }
    B element=b1;
    while (element != null)
    {
    System.out.println(element.x + ",");
    element=element.ref;

    }

    }
    }

    -the program is throwing one exception;i.e; Exception in thread "main" java.lang.NullPointerException: Cannot assign field "ref" because "<local6>" is null
    at MyClass.main(MyClass.java:27).
    this exception was caused because it should have been "element.ref" in the validation statement of the while loop which is inside the else block.

    Can someone explain me why?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me understand why i am getting this error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.


    element is null when the code exits the while loop. It can not be used to reference the variable: ref
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: can someone help me understand why i am getting this error.

    by input:
    10 20
     
    you get:
      new b1( x=10; ref=null)
     
      element = b1
      while() {
        element = b1.ref // null
      }
      elelement.ref =    // error element is null

Similar Threads

  1. Don't understand the error
    By fredclaus in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2019, 09:33 PM
  2. [SOLVED] I have a NullPointerException error that I dont understand
    By LukeR in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2013, 06:15 PM
  3. JButton, JTable error not understand.
    By kpat in forum AWT / Java Swing
    Replies: 1
    Last Post: April 11th, 2012, 02:37 PM
  4. I can't understand the error.
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 21st, 2011, 01:56 PM
  5. I can''t understand this error
    By ragingdemon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 8th, 2011, 06:07 PM