0

So I have this piece of Java code:

if (e1.getNodeName().contains("column")) {
    String temp_type = "";
    String temp_index = "";
    String name = "";
    String format = "";
    int index = 0;
    DataType type = null;

    try {
        name = e1.getElementsByTagName("name").item(0).getTextContent();
        System.out.println("Name: " + name);
    } catch (NullPointerException e) {
        System.out.println("No name");
    }

    try {
        temp_index = e1.getAttributes().getNamedItem("index").getNodeValue();
        index = Integer.parseInt(temp_index);
        System.out.println("Index: " + index);
    } catch (NullPointerException e) {
        System.out.println("No index");
    }

    try {
        temp_type = e1.getElementsByTagName("type").item(0).getTextContent();
        try {
            type= DataType.typeOf(temp_type);
        } catch (UnknownDataTypeException e) {
            System.out.println("Unknown Data Type");
        }
        System.out.println("Type: " + type);
    } catch (NullPointerException e) {
        System.out.println("No type");
    }

    try {
        format = e1.getAttributes().getNamedItem("format").getNodeValue();
        System.out.println("Format: " + format);
    } catch (NullPointerException e) {
        System.out.println("No format");
    }

I am reading an XML file by implementing a DOM parser. I have a sample XML file and ever entry DOES NOT contain all of the following values (name is missing for the first entry). When I run this piece of code, everything prints out perfectly and I am able to handle the errors exactly how I want to.

My next step is to use each of these values (name, index, DataType, format) to create a Person object which I have defined in a previous class. I thus having the following piece of code to create a Person object and add it to an ArrayList (personList):

Person c = new Person(name, index, type, format);
personList.add(c);

Those two lines above give me a NullPointerException, even though I handle the thrown errors and I define initial values for my instance variables name, format, index and type at the beginning of the code.

Why doesn't Java see that I have already dealt with those errors and allow me to properly create a Person object?

I deeply appreciate all help.

Below is my Person class:

public class Person {
    public String name;
    public int index;
    public DataType dataType;
    public String dateFormat;

    public Person(String name, int index, DataType dataType, String dateFormat) {
        this.name = name;
        this.index = index;
        this.dataType = dataType;
        this.dateFormat = dateFormat;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }

    public DataType getDataType() {
        return dataType;
    }

    public String getDateFormat() {
        return dateFormat;
    }
    enter code here

}
Nicholas Chen
  • 541
  • 5
  • 13
  • 1
    You don't show that line in this code; but my guess is that `personList` is null because you've not instantiated it. – Andy Turner Jun 26 '17 at 20:55
  • Why would personList being null have to do with the NullPointerException when I am creating the Person object? – Nicholas Chen Jun 26 '17 at 20:55
  • OK, so the way you'd written it (without the 4 space indent) it looked like it was 1 line. Then... it's something in the constructor. You've not (obviously) shown the code for the constructor, so it's impossible to say. – Andy Turner Jun 26 '17 at 20:57
  • 2
    By the way: catching `NullPointerException` is almost *never* the right thing to do. You should avoid doing the thing which causes the NPE in the first place. – Andy Turner Jun 26 '17 at 20:57
  • but that's just how DOM parsers work with the following methods which I use. – Nicholas Chen Jun 26 '17 at 20:58
  • Show us your `Person` class please. – Marvin Jun 26 '17 at 20:58
  • No: `NullPointerException`s happen because you attempt to dereference a null reference; you avoid it by not doing that. If `foo.bar()` throws a NPE, change that to `if (foo != null) foo.bar();` (for example). I literally cannot remember the last time I even considered catching NPE. – Andy Turner Jun 26 '17 at 21:00
  • public class Person { public String name; public int index; public DataType dataType; public String dateFormat; public Person(String name, int index, DataType dataType, String dateFormat) { this.name = name; this.index = index; this.dataType = dataType; this.dateFormat = dateFormat; } public String getName() { return name; } public int getIndex() { return index; } public DataType getDataType() { return dataType; } public String getDateFormat() { return dateFormat } } – Nicholas Chen Jun 26 '17 at 21:00
  • [Edit] your question to add code. You can't read it in a comment. – Andy Turner Jun 26 '17 at 21:00
  • aight I just edited it. – Nicholas Chen Jun 26 '17 at 21:01
  • That constructor does not throw a `NullPointerException`. If the exception occurs in the lines you have shown, it *must* be in the `personList.add` line: either `personList` is null, or you are using an implementation of `List` which doesn't permit null elements. – Andy Turner Jun 26 '17 at 21:05
  • Ok, yeah actually I debugged the code and ur right, the List implementation doesn't permit null elements. Thank you very much for ur help. – Nicholas Chen Jun 26 '17 at 21:07
  • I'm looking at my code and I really don't understand why not implementing error handling within my constructor would contribute to the problems I am running into. I am nevertheless still able to create a Person object, right? – Nicholas Chen Jun 26 '17 at 22:42

0 Answers0