Sunday, 2 November 2014

Trouble loading from XML file



I have a major problem. We can call this application "boat club system" and its a assignment in a UML course. But the problem that is really bothering me is that i can't load the "Member" Objects back to the Arraylist that is used when Exporting and Importing the objects.


I got some help with this by a friend, but the Importing won't work. This is the Error i get when trying to import.



Exception in thread "main" java.lang.NullPointerException
at boat.Controller.SystemController.readFromSystem(SystemController.java:288)
at boat.Controller.SystemController.<init>(SystemController.java:26)
at boat.View.Console.<init>(Console.java:17)
at BoatMain.main(BoatMain.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


The problem is pointed on this specific line:



member.setMemberId(Integer.parseInt(element.get(i).getAttribute("memberId").getValue()));


The link to the whole application. http://ift.tt/1s56EjX


The System controller class looks like this. BTW, i am thinking of splitting up this class to more like System controller and Member controller for a more High Cohesion/Low coupling design. But only when i get the Import working correctly..


I don't know why it doesn't work. The member id is a INTEGER at first, but when exported its converted to a String. When imported it converted to a Int again. So I'm wondering if its something wrong there..


Would really appreciate help so i could continue. System Controller



public void readFromSystem () {

File file = null;
Builder builder = null;
Document doc = null;

try {

file = new File(filePath);
builder = new Builder();
doc = builder.build(file);

Element root = doc.getRootElement();

Elements members = root.getChildElements();


for (int i = 0; i < members.size(); i++) {

Elements element = members.get(i).getChildElements();
Member member = new Member();

member.setMemberId(Integer.parseInt(element.get(i).getAttribute("memberId").getValue()));
member.setPersonId(element.get(0).getValue());
member.setName(element.get(1).getValue());

memberList.add(member);

if (members.get(i).getChildElements().size() == 3) {

Elements boats = element.get(2).getChildElements();

for (int j = 0; j < boats.size(); j++) {

Boat b = new Boat();

b.setBoatId(Integer.parseInt(boats.get(j).getAttribute("boatId").getValue()));
b.setBoatType(Integer.parseInt(boats.get(j).getChildElements().get(1).getValue()));
b.setBoatLength(boats.get(j).getChildElements().get(2).getValue());

}
}
}
} catch (IOException e) {
System.out.print("Could not read from the system");
} catch (nu.xom.ParsingException e) {
System.out.print("Parsing was unsuccessful!");
}
}

No comments:

Post a Comment