学生fxml控制器
我有一个类似的老师的代码,填充数据库到表视图,它的工作但不知道为什么我在这一个错误指向最后的错误指向 teacherTable.setItems(ban);
请问我该怎么办
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
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)
... 61 more
Caused by: java.lang.NullPointerException
at schoolmanagement.Student.StudentZoneController.load(StudentZoneController.java:65)
... 70 more
package schoolmanagement.Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
public class StudentZoneController {
Connection con;
PreparedStatement p;
String rowid,fname,lname,age,sex,cl;
ResultSet set;
Alert popup;
@FXML
private TableView<usera> teacherTable;
@FXML
private TableColumn<usera, String> ROWID;
@FXML
private TableColumn<usera, String> FIRSTNAME;
@FXML
private TableColumn<usera, String> LASTNAME;
@FXML
private TableColumn<usera, String> AGE;
@FXML
private TableColumn<usera, String> SEX;
@FXML
private TableColumn<usera, String> CLASS;
@FXML
private void load ()
{
ObservableList<usera> ban =FXCollections.observableArrayList( );
teacherTable.setItems(ban);
}
@FXML
private void initialize()
{
ROWID.setCellValueFactory(new PropertyValueFactory<usera,String>("rowid"));
FIRSTNAME.setCellValueFactory(new PropertyValueFactory<usera,String>("firstname"));
LASTNAME.setCellValueFactory(new PropertyValueFactory<usera,String>("lastname"));
AGE.setCellValueFactory(new PropertyValueFactory<usera,String>("age"));
SEX.setCellValueFactory(new PropertyValueFactory<usera,String>("sex"));
CLASS.setCellValueFactory(new PropertyValueFactory<usera,String>("primary"));
}
@FXML
private TextField t1;
@FXML
private TextField t2;
@FXML
private TextField t3;
@FXML
private DatePicker dob;
@FXML
private TextField t4;
@FXML
private TextField t5;
@FXML
private TextField t6;
@FXML
private TextField t7;
@FXML
private TextField t8;
@FXML
private TextField t9;
@FXML
private TextField t10;
@FXML
private TextField t11;
@FXML
private TextField t12;
@FXML
private TextField t13;
@FXML
private void showAge()
{
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int birth = (dob.getValue().getYear());
int age = year-birth;
t4.setText(Integer.toString(age) + "years old");
}
@FXML
private void addNewUser() throws SQLException
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
com.mysql.jdbc.Connection con =(com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://localhost/school?"+"user=root&password =");
p = (com.mysql.jdbc.PreparedStatement)con.prepareStatement("insert into students values(?,?,?,?,?,?)");
p.setString(1, t1.getText());
p.setString(2, t2.getText());
p.setString(3, t3.getText());
p.setString(4, t4.getText());
p.setString(5, t5.getText());
p.setString(6, t6.getText());
popup = new Alert(AlertType.INFORMATION);
popup.setTitle("INFORMATION DIALOG");
popup.setHeaderText(null);
popup.setContentText("user created");
popup.showAndWait();
p.executeUpdate();
p.close();
clearFields();
}
catch(Exception eb)
{
}
}
@FXML
private void deleteStudent()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
com.mysql.jdbc.Connection con =(com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://localhost/school?"+"user=root&password =");
p = (com.mysql.jdbc.PreparedStatement)con.prepareStatement("delete from students where FIRSTNAME = ? || LASTNAME = ?");
p.setString(1, t2.getText());
p.setString(2, t3.getText());
popup = new Alert(AlertType.INFORMATION);
popup.setTitle("INFORMATION DIALOG");
popup.setHeaderText(null);
popup.setContentText("STUDENT DELETED");
popup.showAndWait();
refreshDelete();
p.executeUpdate();
}
catch(Exception l)
{
popup = new Alert(AlertType.INFORMATION);
popup.setTitle("INFORMATION DIALOG");
popup.setHeaderText(null);
popup.setContentText(l.getMessage());
popup.showAndWait();
}
}
private void clearFields()
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
dob.setValue(null);
}
@FXML
private void resetFields()
{
t9.clear();
t10.clear();
t11.clear();
t12.clear();
t13.clear();
}
private void refreshDelete()
{
t2.clear();
t3.clear();
}
}