1
import java.io.*;

public class workPoS {

    /**
     * @param args
     */
    public static void main(String[] args) {
        dataList[] some_list = new dataList[10];

        for (int x = 0; x < some_list.length; x++) {
            some_list[x].firstName = "John";
            some_list[x].middleName = "Jacob";
            some_list[x].lastName = "Jingle-Heimer-Schmidt";
            some_list[x].age = 101;
        }

        for (int x = 0; x < some_list.length; x++) {
            System.out.println(some_list[x].firstName + " "
                    + some_list[x].middleName + " " + some_list[x].lastName
                    + " Age: " + some_list[x].age);
        }

    }

    public class dataList {
        String firstName, middleName, lastName;
        int age;

        public dataList() {
            firstName = "";
            middleName = "";
            lastName = "";
            age = 0;
        }
    }

}

ERROR: Exception in thread "main" java.lang.NullPointerException at workPoS.main(workPoS.java:12)

This is line 12:

some_list[x].firstName = "John";

It seems that I am able to access some_list[x], but as soon as I specify a constructor, some_list[x].firstName, the result is a null value.

Am I simply referencing the constructor value incorrectly? If so, what is the correct way to do so?

Thanks guys!

1
  • is android an accurate tag? The entrance point for an Android application isn't main like it is for a desktop application. Commented Mar 7, 2012 at 20:16

2 Answers 2

7

Look here:

dataList[] some_list = new dataList[10];

for (int x = 0; x < some_list.length; x++) {
    some_list[x].firstName = "John";

You've created the array - but all the elements will be null references. You talk about "referencing the constructor value incorrectly" - you're not calling the constructor at all. You should have:

for (int x = 0; x < some_list.length; x++) {
    some_list[x] = new dataList();
    some_list[x].firstName = "John";

See the Java Tutorial for Arrays for more information.

(You should also fix your names to follow Java naming conventions, and make your fields private, but that's a different matter...)

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using both the classes in the same .java file, the try this code

import java.io.*;

public class workPoS {

public workPoS() { 
  super(); 

}

/** * @param args */ public static void main(String[] args) { workPoS wp = new workPoS(); dataList[] some_list = new dataList[10];

    for (int x = 0; x < some_list.length; x++) {
      some_list[x] = wp.new dataList();
        some_list[x].firstName = "John";
        some_list[x].middleName = "Jacob";
        some_list[x].lastName = "Jingle-Heimer-Schmidt";
        some_list[x].age = 101;
    }

    for (int x = 0; x < some_list.length; x++) {
        System.out.println(some_list[x].firstName + " "
                + some_list[x].middleName + " " + some_list[x].lastName
                + " Age: " + some_list[x].age);
    }

}

public class dataList {
    String firstName, middleName, lastName;
    int age;

    public dataList() {
        firstName = "";
        middleName = "";
        lastName = "";
        age = 0;
    }
}

}

1 Comment

Awesome! Thank you for actually taking the time to solve the problem rather than assume I'm a complete idiot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.