我正在按照这个答案,在制作自己的Listview
时,除了在我尝试更新我的nullpointer
时给我一个listview
时,一切都很顺利,当它尝试时会发生这种情况在TextFields
中将数据设置到我的ListCell
这里是我的Controller类代码
private ArrayList <RoadItem> roadListItem = new ArrayList<>();
private ObservableList observableList = FXCollections.observableArrayList();
public void setRoadListView()
{
RoadItem item = new RoadItem();
item.setDirection("left");
item.setRegNo(""+2325);
item.setWidth(345);
item.setSpeed(233);
roadListItem.add(item);
observableList.setAll(roadListItem);
roadItemListView.setItems(observableList);
roadItemListView.setCellFactory(new Callback<ListView<RoadItem>, javafx.scene.control.ListCell<RoadItem>>()
{
@Override
public ListCell<RoadItem> call(ListView<RoadItem> arg0) {
// TODO Auto-generated method stub
return new RoadListCell();
}
});
} 我的RoadItem类
@FXML ImageView imageView;
@FXML TextField txt_plateNumber;
@FXML TextField txt_speed;
@FXML TextField txt_width;
@FXML TextField txt_direction;
@FXML AnchorPane anchorPane;
public RoadListItem()
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/application/carsListCell.fxml"));
fxmlLoader.setController(this);
try
{
fxmlLoader.load();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void setCellItem(RoadItem item )
{
//Where NullPointer is occuring
if(item.getImage() != null)
imageView.setImage(item.getImage());
txt_plateNumber.setText(""+item.getRegNo());
txt_speed.setText(""+item.getSpeed()+"Km/h");
txt_width.setText(""+item.getWidth()+"mtrs");
txt_direction.setText(item.getDirection());
}
public AnchorPane getAnchorPane()
{
return this.anchorPane;
}
我的RoadListCell类
public class RoadListCell extends ListCell<RoadItem>
{
public void updateItem(RoadItem item, boolean empty)
{
super.updateItem(item, empty);
if(item != null)
{
RoadListItem listItem = new RoadListItem();
listItem.setCellItem(item);
setGraphic(listItem.anchorPane);
}
}
RoadItem类
public class RoadItem {
private Image image;
private String regNo;
private float speed;
private float width;
private String direction;
public RoadItem() {
super();
this.image = new Image("assets/eyeMediumsec.png");;
this.regNo = "";
this.speed = 0;
this.width = 0;
this.direction = "";
}
public RoadItem(Image image, String regNo, float speed, float width, String direction) {
super();
this.image = image;
this.regNo = regNo;
this.speed = speed;
this.width = width;
this.direction = direction;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public String getRegNo() {
return regNo;
}
public void setRegNo(String regNo) {
this.regNo = regNo;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
My Cell FXML
<AnchorPane fx:id="anchorPane" prefHeight="60.0" prefWidth="350.0"
xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" >
<children>
<Card prefHeight="60.0" prefWidth="350.0">
<children>
<Region prefHeight="60.0" prefWidth="300.0" />
<HBox prefHeight="60.0" prefWidth="350.0">
<children>
<ImageView fx:id ="imageView" fitHeight="60.0"
fitWidth="60.0" pickOnBounds="true">
<image>
<Image url="@../../../../Downloads/african_sad_girl_by_psymfonius-d4bglwk.jpg" />
</image>
</ImageView>
<Separator maxWidth="1.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="1.0" scaleX="0.5" />
<VBox prefHeight="200.0" prefWidth="110.0">
<children>
<Label text="Plate No:" translateX="6.0">
<font>
<Font size="10.0" />
</font></Label>
<Separator prefWidth="200.0" scaleY="0.5" />
<TextInput id="txt_plateNumber" translateY="3.0" />
</children>
</VBox>
<Separator maxWidth="6.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="2.0" scaleX="0.5" />
<VBox prefHeight="200.0" prefWidth="110.0">
<children>
<Label text="Speed" translateX="12.0">
<font>
<Font size="10.0" />
</font>
</Label>
<Separator prefWidth="200.0" scaleY="0.5" />
<TextInput id="txt_speed" translateY="3.0" />
</children>
</VBox>
<Separator maxWidth="1.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="1.0" scaleX="0.5" />
<VBox prefHeight="200.0" prefWidth="110.0">
<children>
<Label text="Width" translateX="12.0">
<font>
<Font size="10.0" />
</font>
</Label>
<Separator prefWidth="200.0" scaleY="0.5" />
<TextInput id="txt_width" translateY="3.0" />
</children>
</VBox>
<Separator maxWidth="1.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="1.0" scaleX="0.5" />
<VBox prefHeight="200.0" prefWidth="110.0">
<children>
<Label text="Direction" translateX="6.0">
<font>
<Font size="10.0" />
</font>
</Label>
<Separator prefWidth="200.0" scaleY="0.5" />
<TextInput id="txt_direction" translateY="3.0" />
</children>
</VBox>
<Separator maxWidth="1.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="1.0" scaleX="0.5" />
<VBox prefHeight="200.0" prefWidth="110.0">
<children>
<Label text="Type" translateX="12.0">
<font>
<Font size="10.0" />
</font>
</Label>
<Separator prefWidth="200.0" scaleY="0.5" />
<TextInput id="txt_vehicleType" translateY="3.0" />
</children>
</VBox>
<Separator maxWidth="6.0" opacity="0.0" orientation="VERTICAL" prefHeight="60.0" prefWidth="4.0" />
</children>
</HBox>
</children>
</Card>
</children>
</AnchorPane>
我的StackTrace
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at adapters.RoadListItem.setCellItem(RoadListItem.java:44)
at adapters.RoadListCell.updateItem(RoadListCell.java:14)
at adapters.RoadListCell.updateItem(RoadListCell.java:1)
at javafx.scene.control.ListCell.updateItem(Unknown Source)
at javafx.scene.control.ListCell.indexChanged(Unknown Source)
at javafx.scene.control.IndexedCell.updateIndex(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Scene.doLayoutPass(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at loginManager.LoginManager.showMainView(LoginManager.java:151)
at loginManager.LoginManager.authenticated(LoginManager.java:51)
at application.LoginController$1.handle(LoginController.java:67)
at application.LoginController$1.handle(LoginController.java:1)
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.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.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)
答案 0 :(得分:0)
RoadItem是默认值设置的图像,还是仍然为空?如果为null - 那就是proplem observableList.setAll(roadListItem); - 按项目设置,并且在roadListItem中有项目,这不是null,但是imageView - null这就是为什么我们有NullPointerException