I'm currently working on a JavaFX project to read data from a file containing information regarding various scenes from a play. I'm trying to display said information on the next scene, but before I implement that, I decided to test and make sure everything is being assigned properly; of course it isn't.
I keep getting a very long error message that I don't know what to make of, but what seems to make sense is the java.lang.NullPointerException. I made a loop to print out the values of my Scene object to see if they were null, but they all contain values. I don't understand why it is considered null, all is fine until I decided to add the newly made Scene into my Scene array. Any help would be appreciated =).
Also switchAct1 in the controller is making the call to the problematic method.
ERROR MESSAGE
1 "Alexander Hamilton" Burr
1 "Alexander Hamilton" Laurens
1 "Alexander Hamilton" Jefferson
1 "Alexander Hamilton" Madison
1 "Alexander Hamilton" Hamilton
1 "Alexander Hamilton" Eliza
1 "Alexander Hamilton" Washington
1 "Alexander Hamilton" Company
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:941)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at model.Act.loadAct(Act.java:88)
at controller.RollController.switchAct1(RollController.java:63)
... 58 more
Act.java
package model;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Act {
String num;
ArrayList<Scene> lists;
Act(String num, ArrayList<Scene> list) {
}
public Act() {
}
public static void setList2(ArrayList<Scene> list) {
list = new ArrayList<>();
}
@SuppressWarnings("resource")
public void loadAct(String file) throws IOException {
BufferedReader br = null;
int i =1;
br = new BufferedReader(new FileReader( file));
String line;
//setList2(this.lists);
while ((line = br.readLine()) != null) {
String [] value = line.split(",");
//split the first value because that includes the act number, title, and the first role
String [] tmp = value[0].split("-");
this.num = tmp[0];
String name = null;
//the last act in the text file contains an additional hyphen
if (tmp.length == 4) {
name = tmp[1] "-" tmp[2];
}
else {
name = tmp[1];
}
Role roleP = null;
//if split into 3, index 2 is the first role
if (tmp.length == 3) {
roleP = new Role (tmp[2]);
}
else if (tmp.length == 4) {
roleP = new Role (tmp[3]);
}
ArrayList<Role> roles = new ArrayList<Role>();
roles.add(roleP);
//assigning the split values into role objects and adding to the role array list
while(i < value.length) {
Role temp = new Role (value [i]);
//System.out.println(temp.name);
roles.add(temp);
i;
}
//creates scenes
Scene scenes = new Scene(tmp[0], name, roles);
//test to make sure values arent null
int j =0;
while (j < scenes.list.size()) {
System.out.println(scenes.id scenes.title scenes.list.get(j).name);
j;
}
//this little bastard line of code is wasting my time
this.lists.add(scenes);
}
}
}
Scene.java
package model;
import java.util.ArrayList;
public class Scene {
String id;
String title;
ArrayList<Role> list;
@SuppressWarnings("unchecked")
Scene(String id, String title,ArrayList<Role> list) {
this.id = id;
this.title = title;
//something i saw online to copy arraylists
this.list = (ArrayList<Role>)list.clone();
}
public static void setList(ArrayList<Role> list) {
list = new ArrayList<>();
}
}
RollController.java
package controller;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import model.Act;
//extend
public class RollController{
private Stage stage;
private Scene scene;
private Parent layout;
@FXML
private TextArea name;
public static TextArea static_name;
@FXML
private TextArea roles;
public static TextArea static_roles;
@FXML
private Button act1, act2;
@FXML
private Label userName;
public static Label static_user;
@FXML
private Button log;
public void initialize () {
static_name = name;
static_roles = roles;
static_user = userName;
}
public void switchScene1 (ActionEvent event) throws IOException {
Parent layout = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(layout);
stage.setTitle("Login");
stage.setScene(scene);
stage.show();
}
public void switchAct1 (ActionEvent event) throws IOException {
Act act1 = new Act();
act1.loadAct("Data\\act1.txt");
Parent layout = FXMLLoader.load(getClass().getResource("/view/ActView.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(layout);
stage.setTitle("Act 1");
stage.setScene(scene);
stage.show();
}
}
TextFile
1 - "Alexander Hamilton" - Burr, Laurens, Jefferson, Madison, Hamilton, Eliza, Washington, Company
2 - "Aaron Burr Sir" - Hamilton, Burr, Laurens, Lafayette, Mulligan, Company
3 - "My Shot" - Hamilton, Laurens, Lafayette, Mulligan, Burr, Company
4 - "The Story of Tonight" - Hamilton, Laurens, Mulligan, Lafayette, Company
5 - "The Schuyler Sisters" - Angelica, Eliza, Peggy, Burr, Company
6 - "Farmer Refuted" - Seabury, Hamilton, Burr, Company
7 - "You'll Be Back" - King George III, Company
8 - "Right Hand Man" - Washington, Hamilton, Burr, Company
9 - "A Winter's Ball" - Burr, Hamilton, Company
10 - "Helpless" - Eliza, Company
11 - "Satisfied" - Angelica , Company
12 - "The Story of Tonight (Reprise)" - Laurens, Mulligan, Lafayette, Hamilton, Burr
13 - "Wait for It" - Burr, Company
14 - "Stay Alive" - Hamilton, Washington, Laurens, Lafayette, Mulligan, Lee, Eliza, Angelica, Company
15 - "Ten Duel Commandments" - Laurens, Hamilton, Lee, Burr, Company
16 - "Meet Me Inside" - Hamilton, Burr, Laurens, Washington, Company
17 - "That Would Be Enough" - Eliza , Hamilton
18 - "Guns and Ships" - Burr, Lafayette, Washington, Company
19 - "History Has Its Eyes on You" - Washington, Hamilton, Company
20 - "Yorktown (The World Turned Upside Down)" - Hamilton, Lafayette, Laurens, Mulligan, Washington, Company
21 - "What Comes Next?" - King George III
22 - "Dear Theodosia" - Burr, Hamilton
23 - "Tomorrow There'll Be More of Us" - Laurens, Eliza, Hamilton
24 - "Non-Stop" - Burr, Hamilton, Angelica, Eliza, Washington, Company
CodePudding user response:
I don't understand why it is considered null,
It is "considered" null
because it is null
. The Java runtime does not lie1.
1 - If you don't believe it, check this by adding a trace print immediately before the offending line to print out what the variable actually contains.
You are getting an NPE (NullPointerException
) because lists
is null
. It is null
because your code doesn't initialize it.
(The other exceptions are a consequence of your code not handling the NPE ... which it probably shouldn't do. They will go away when your code no longer throws NPEs on the application thread. IMO, you don't need to do anything about them.)
It looks like you have been trying to initialize lists
in various places, but you are making various mistakes.
The constructors have empty bodies. So the one that takes a
list
argument is ignoring it.The
static
methodsetList2
should not be static if it is your intent that it can be used to setlists
. A static method can't assign to an instance variable ofthis
... explicitly or implicitly.The
setList2
method is assigning tolist
notlists
. That is the name of the parameter. So what it is actually doing is creating an emptyArrayList
and assigning it to a local variable ... which then just disappears.Your are not using either the
Act(String num, ArrayList<Scene> list)
constructor or setList2. The call to the latter is commented out.
Advice:
Make sure that you understand the differences between regular and
static
methods. I mean really understand. (The mistakes you made in yoursetList2
method imply that you don't understand.) Go back and read your textbook, lecture notes, whatever again. Or find a good Java tutorial that explains it.Avoid static methods, and static fields. They are usually not the solution.
Read your code, and error message carefully; e.g. there were probably clues at some point that would have told you that
list
andlists
are not the same name.If you get an compilation error don't just try to make the error go away with some random change; e.g. commenting out the code. You actually need to understand the what the error message is telling you and work out the correct way to fix it.
The "Programming by randomly changing things" technique is incredibly inefficient ... and frustrating ... and frequently ends up producing bizarre and broken code. So, don't do that. Instead, you might want to read about the Rubber Duck Debugging technique.
Using the Rubber Duck Debugging technique the Duck would have wanted to know why you were so sure that lists
was not null
. In explaining it to the Duck, you would have noticed the obvious: that nowhere in your code do you assign anything to list
.