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 2 of 2

Thread: Java NullPointerException When Accessing Object Attributes

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Java NullPointerException When Accessing Object Attributes

    I'm encountering a NullPointerException in my Java code when trying to access the attributes of an object. I've created an instance of a class and assigned values to its attributes, but I'm still getting this error. Here's a simplified version of my code:

    public class Student {
        String name;
        int age;
     
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
     
    public class Main {
        public static void main(String[] args) {
            Student student = new Student("Alice", 20);
            System.out.println("Name: " + student.name);
            System.out.println("Age: " + student.age);
     
            // Some other code here
        }
    }

    Surprisingly, the NullPointerException occurs at the System.out.println() lines. I've confirmed that the student object is not null, and I've also verified that the name and age attributes have been assigned values in the constructor.

    What could be causing this error, and how can I fix it? Is there something else I should be looking out for when working with object attributes? Any assistance would be greatly appreciated. Thank you!

  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: Java NullPointerException When Accessing Object Attributes

    The posted code works for me.

    Can you copy and paste here the full contents of the window from when the exception happens?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Accessing deeper levels of JSON object
    By ninjakreborn in forum Computer Support
    Replies: 1
    Last Post: August 16th, 2021, 08:23 AM
  2. Accessing java application without adding site to java security
    By balaskandhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2014, 09:21 AM
  3. How do you store attributes for a java text file in the file itself?
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2013, 08:22 PM
  4. Get CSS Attributes using Java
    By justinmifsud in forum Object Oriented Programming
    Replies: 2
    Last Post: February 15th, 2011, 05:08 PM
  5. [SOLVED] Accessing methods of an object inside in ArrayList
    By jmack in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 12:28 PM

Tags for this Thread