我正在尝试更改面板内标签的文本,但每当我尝试对该面板的组件进行任何更改时,我都会收到错误NullPointer。 我的程序使用工作表来存储数据并显示给用户
FXMLDocumetController.java(我使用'alterarTela(int tela)'方法在面板之间切换):
package agenda;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXTextField;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
public class FXMLDocumentController implements Initializable {
@FXML
private AnchorPane paneFundo;
@FXML
private JFXListView<Label> listView;
@FXML
private JFXTextField txtPesquisar;
@FXML
private JFXButton btnPesquisar;
@FXML
private JFXButton FAB;
ArrayList <String> urlTelas = new ArrayList<String>();
Arquivo arquivo = new Arquivo();
PaneInfoPessoa paneInfoPessoa = new PaneInfoPessoa();
PaneInfoPA paneInfoPa = new PaneInfoPA();
@FXML
public void btnPesquisarClique (ActionEvent e) {
}
@FXML
public void FABClique (ActionEvent e) throws IOException {
System.out.println("Tela add abrir");
alterarTela(2);
}
@FXML
public void itemListaSelecionado() {
String nome[] = listView.getSelectionModel().getSelectedItem().getText().split("\n");
if (nome[0].startsWith("PA")) {
alterarTela(1);
System.out.println("Tela alterada 1");
paneInfoPa.atualizarDados(arquivo.pegarPaLista(nome[0]));
} else {
alterarTela(0);
System.out.println("Tela alterada 0");
paneInfoPessoa.atualizarDados(arquivo.pegarPessoaLista(nome[0]));
}
}
public void alterarTela(int tela) {
try {
Pane pane = FXMLLoader.load(getClass().getResource(urlTelas.get(tela)));
paneFundo.getChildren().setAll(pane);
} catch (IOException e) {
System.out.print("Erro na função alterar tela " + e);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
urlTelas.add("PaneInfoPessoa.fxml");
urlTelas.add("PaneInfoPA.fxml");
urlTelas.add("PaneAdd.fxml");
listView.setItems(arquivo.lerPlanilha(0, true));
listView.setFixedCellSize(60);
listView.setFocusTraversable(false);
listView.getSelectionModel().selectedItemProperty().addListener(
(Observable, oldValue, newValue) -> itemListaSelecionado());
}
}
Arquivo.java(用于设置和从工作表中获取数据):
package agenda;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Label;
import jxl.Sheet;
public class Arquivo {
private List<Label> listaContatos = new ArrayList<>();
private ObservableList<Label> obsListaContatos;
public ObservableList<Label> lerPlanilha(int num_planilha, boolean lerTodas) {
if (lerTodas) {
try {
int contPlanilhas = Agenda.workbook.getNumberOfSheets();
for (int j = 0; j < contPlanilhas; j++ ) {
List<Label> listaContatos2 = new ArrayList<>();
Sheet planilha = Agenda.workbook.getSheet(j);
int linhas = planilha.getRows();
for (int i = 0; i < linhas; i++) {
String colunaNome = planilha.getCell(0, i).getContents();
String colunaFixo = planilha.getCell(2, i).getContents();
listaContatos2.add(new Label(colunaNome + "\n" + colunaFixo));
}
//TODO ordenar
listaContatos.addAll(listaContatos2);
}
obsListaContatos = FXCollections.observableArrayList(listaContatos);
return obsListaContatos;
} catch (Exception e) {
System.out.println("Erro na função lerPlanilha" + e.toString());
return null;
}
} else {
try {
Sheet planilha = Agenda.workbook.getSheet(num_planilha);
int linhas = planilha.getRows();
for (int i = 0; i < linhas; i++) {
String colunaNome = planilha.getCell(0, i).getContents();
String colunaFixo = planilha.getCell(2, i).getContents();
listaContatos.add(new Label(colunaNome + "\n" + colunaFixo));
}
obsListaContatos = FXCollections.observableArrayList(listaContatos);
return obsListaContatos;
} catch (Exception e) {
System.out.println("Erro na função lerPlanilha" + e.toString());
return null;
}
}
}
public Pessoa pegarPessoaLista(String nome) {
int linhaSel = -1;
Pessoa pessoa = new Pessoa();
Sheet planilha = Agenda.workbook.getSheet(1);
int linhas = planilha.getRows();
for (int i = 0; i < linhas; i++) {
if (planilha.getCell(0, i).getContents().equals(nome)) {
linhaSel = i;
}
}
if (linhaSel >= 0) {
pessoa.setNome(planilha.getCell(0, linhaSel).getContents());
pessoa.setRamal(planilha.getCell(1, linhaSel).getContents());
pessoa.setCelular(planilha.getCell(2, linhaSel).getContents());
pessoa.setTelefone(planilha.getCell(3, linhaSel).getContents());
pessoa.setPa(planilha.getCell(4, linhaSel).getContents());
pessoa.setSetor(planilha.getCell(5, linhaSel).getContents());
pessoa.setEmail(planilha.getCell(6, linhaSel).getContents());
return pessoa;
} else {
pessoa.setNome("Erro");
pessoa.setPa("");
pessoa.setCelular("");
pessoa.setTelefone("");
pessoa.setRamal("");
pessoa.setSetor("");
pessoa.setEmail("");
System.out.println("Pessoa não encontrada na funcao arquivo.pegarPessoaLista");
return pessoa;
}
}
public Pessoa pegarPessoaPesquisa() {
return null;
}
public Pa pegarPaLista(String pa) {
int linhaSel = -1;
Pa PA = new Pa();
Sheet planilha = Agenda.workbook.getSheet(0);
int linhas = planilha.getRows();
for (int i = 0; i < linhas; i++) {
if (planilha.getCell(0, i).getContents().equals(pa)) {
linhaSel = i;
}
}
if (linhaSel >= 0) {
PA.setNumero(planilha.getCell(0, linhaSel).getContents());
PA.setRamal(planilha.getCell(1, linhaSel).getContents());
PA.setFixo1(planilha.getCell(2, linhaSel).getContents());
PA.setFixo2(planilha.getCell(3, linhaSel).getContents());
PA.setCelular(planilha.getCell(4, linhaSel).getContents());
PA.setContPessoas(planilha.getCell(5, linhaSel).getContents());
PA.setGerente(planilha.getCell(6, linhaSel).getContents());
PA.setLocal(planilha.getCell(0, linhaSel).getContents());
System.out.println("Pa selecionado" + PA.getNumero());
return PA;
} else {
PA.setNumero("Erro");
PA.setRamal("");
PA.setFixo1("");
PA.setFixo2("");
PA.setCelular("");
PA.setContPessoas("");
PA.setGerente("");
PA.setLocal("");
System.out.println("Pessoa não encontrada na funcao arquivo.pegarPessoaLista");
return PA;
}
}
public Pa pegarPaPesquisa() {
return null;
}
}
PaneInfoPa.java(PaneInfoPa.FXML的fx控制器):
package agenda;
import com.jfoenix.controls.JFXButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
public class PaneInfoPA {
@FXML
private JFXButton btnEditar;
@FXML
private JFXButton btnRemover;
@FXML
private Label txtNumero;
@FXML
private Label txtRamal;
@FXML
private Label txtTelefone;
@FXML
private Label txtTelefone2;
@FXML
private Label txtCelular;
@FXML
private Label txtContPessoas;
@FXML
private Label txtGerente;
@FXML
private void initialize() {
System.out.println("Iniciando tela");
txtNumero.setText("--");
txtRamal.setText("--");
txtTelefone.setText("--");
txtTelefone2.setText("--");
txtCelular.setText("--");
txtContPessoas.setText("--");
txtGerente.setText("--");
System.out.println("tela iniciada");
}
public void atualizarDados(Pa pa) {
System.out.println("Alterando Valores do pane info pa - " + pa.getNumero());
btnEditar.setDisable(true);
//txtNumero.setText(pa.getNumero());
/*txtRamal.setText(pa.getRamal());
txtTelefone.setText(pa.getFixo1());
txtTelefone2.setText(pa.getFixo2());
txtCelular.setText(pa.getCelular());
txtContPessoas.setText(pa.getContPessoas());
txtGerente.setText(pa.getGerente());*/
System.out.println("Valores do pane info pa atualizados");
}
@FXML
public void txtGerenteClique(ActionEvent event) {
System.out.println("btn editar clique");
}
@FXML
public void btnEditarClique(ActionEvent event) {
System.out.println("btn editar clique");
}
@FXML
public void btnRemoverClique(ActionEvent event) {
System.out.println("btn remover clique");
}
}
PaneInfoPa.FXML( 我已经检查了FXML的所有fx:id:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="437.0" prefWidth="418.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="agenda.PaneInfoPA">
<children>
<Pane layoutX="23.0" layoutY="18.0" prefHeight="400.0" prefWidth="376.0" style="-fx-background-color: #ffffff; -fx-background-radius: 5 5 5 5;">
<effect>
<DropShadow color="#00000037" />
</effect>
<children>
<Pane layoutX="18.0" layoutY="240.0" prefHeight="55.0" prefWidth="188.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Celular" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtCelular" layoutX="10.0" layoutY="27.0" prefHeight="24.0" prefWidth="161.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutX="15.0" layoutY="84.0" prefHeight="55.0" prefWidth="261.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Número / Local" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtNumero" layoutX="10.0" layoutY="26.0" prefHeight="24.0" prefWidth="241.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children></Pane>
<Label layoutX="18.0" layoutY="23.0" text="Informações">
<font>
<Font name="Segoe UI" size="22.0" />
</font>
</Label>
<Separator layoutX="15.0" layoutY="57.0" prefHeight="10.0" prefWidth="339.0" />
<JFXButton fx:id="btnEditar" layoutX="213.0" layoutY="22.0" onAction="#btnEditarClique" prefHeight="27.0" prefWidth="66.0" ripplerFill="#c6c6c6" text="Editar" textFill="#16a085">
<font>
<Font name="Segoe UI Semibold" size="13.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnRemover" layoutX="282.0" layoutY="22.0" onAction="#btnRemoverClique" prefHeight="27.0" prefWidth="74.0" ripplerFill="#cdcdcd" text="Remover" textFill="#e74c3c">
<font>
<Font name="Segoe UI Semibold" size="13.0" />
</font>
</JFXButton>
<Pane layoutX="15.0" layoutY="163.0" prefHeight="55.0" prefWidth="161.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Telefone 1" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtTelefone" layoutX="10.0" layoutY="27.0" prefHeight="22.0" prefWidth="144.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutX="191.0" layoutY="163.0" prefHeight="55.0" prefWidth="167.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Telefone 2" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtTelefone2" layoutX="10.0" layoutY="27.0" prefHeight="22.0" prefWidth="144.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutX="287.0" layoutY="84.0" prefHeight="55.0" prefWidth="66.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Ramal" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtRamal" layoutX="10.0" layoutY="27.0" prefHeight="24.0" prefWidth="46.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutX="225.0" layoutY="240.0" prefHeight="55.0" prefWidth="131.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Nº de pessoas" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtContPessoas" layoutX="10.0" layoutY="27.0" prefHeight="24.0" prefWidth="103.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutX="18.0" layoutY="319.0" prefHeight="55.0" prefWidth="261.0" style="-fx-background-color: fafafa; -fx-background-radius: 5 5 5 5;">
<children>
<Label layoutX="10.0" layoutY="5.0" text="Gerente" textFill="#16a085">
<font>
<Font name="Segoe UI" size="15.0" />
</font>
</Label>
<Label fx:id="txtGerente" layoutX="10.0" layoutY="27.0" onMouseClicked="#txtGerenteClique" prefHeight="24.0" prefWidth="241.0" text="--">
<font>
<Font name="Segoe UI" size="19.0" />
</font>
</Label>
</children>
</Pane>
</children>
</Pane>
</children>
</AnchorPane>
FXMLDocument.FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="437.0" prefWidth="725.0" style="-fx-background-color: #f1f1f1;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="agenda.FXMLDocumentController">
<children>
<ImageView fitHeight="437.0" fitWidth="418.0" opacity="0.39" pickOnBounds="true">
<image>
<Image url="@../img/grafismo.jpg" />
</image>
</ImageView>
<Pane layoutX="418.0" prefHeight="437.0" prefWidth="307.0" style="-fx-background-color: #ffffff;">
<children>
<JFXTextField fx:id="txtPesquisar" focusColor="WHITE" layoutX="64.0" layoutY="14.0" prefHeight="39.0" prefWidth="224.0" promptText="Pesquisar" unFocusColor="WHITE">
<font>
<Font size="18.0" />
</font>
</JFXTextField>
<Separator layoutX="24.0" layoutY="36.0" prefHeight="39.0" prefWidth="265.0" />
<JFXButton fx:id="btnPesquisar" layoutX="24.0" layoutY="16.0" onAction="#btnPesquisarClique" prefHeight="33.0" prefWidth="34.0" text="Q" />
<JFXListView fx:id="listView" layoutX="24.0" layoutY="61.0" prefHeight="358.0" prefWidth="278.0" stylesheets="@style.css" />
<JFXButton fx:id="FAB" buttonType="RAISED" layoutX="228.0" layoutY="360.0" onAction="#FABClique" prefHeight="54.0" prefWidth="54.0" ripplerFill="#a8a8a8" style="-fx-background-radius: 50 50 50 50; -fx-background-color: #16a085;" text="+" textFill="WHITE">
<font>
<Font name="System Bold" size="24.0" />
</font>
</JFXButton>
</children>
</Pane>
<AnchorPane fx:id="paneFundo" prefHeight="437.0" prefWidth="418.0" />
</children>
</AnchorPane>
错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at agenda.PaneInfoPA.atualizarDados(PaneInfoPA.java:48)
at agenda.FXMLDocumentController.itemListaSelecionado(FXMLDocumentController.java:54)
at agenda.FXMLDocumentController.lambda$0(FXMLDocumentController.java:82)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
at javafx.scene.control.MultipleSelectionModelBase.lambda$new$34(MultipleSelectionModelBase.java:67)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
at javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:404)
at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:356)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
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.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:394)
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$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)