启动错误-由以下原因引起:java.lang.NullPointerException

时间:2020-03-28 22:39:43

标签: java nullpointerexception

请帮我解决这个问题。

*Caused by: java.lang.NullPointerException
    at main.PanelBatohu.<init>(PanelBatohu.java:19)
    at main.Tajemny_ostrov_Adventura.start(Tajemny_ostrov_Adventura.java:79)*

它指的是:

public PanelBatohu (Batoh batoh) {
        this.batoh = batoh;
        batoh.zaregistrujPozorovatele(this);
        init();
    }

    **panelBatohu = new PanelBatohu(hra.getBatoh());
    borderPane.setLeft(panelBatohu.getPanel());**

如果我删除最后一部分,程序将开始运行,但是对于这一部分,它不会:(

请,我不知道如何解决它,有人知道吗?

提前谢谢!

最好的问候, 约翰

2 个答案:

答案 0 :(得分:1)

可能未初始化变量hra。您尚未提交所有代码,所以我帮不上什么忙。

答案 1 :(得分:0)

谢谢大家。这就是整个代码。

PanelBatohu:

Traceback (most recent call last):
  File "path/WebScraping.py", line 8, in <module>
    posts = soup.find_all(class_='s-item__wrapper clearfix')
AttributeError: 'bytes' object has no attribute 'find_all'

和Tajemny_ostrov_Adventura(开始课程):

package main;

import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import logika.Batoh;
import util.ObserverZmenyBatohu;


public class PanelBatohu implements ObserverZmenyBatohu {
    private VBox vBox;
    private FlowPane panelVeci;
    private Batoh batoh;

    public PanelBatohu (Batoh batoh) {
        this.batoh = batoh;
        batoh.zaregistrujPozorovatele(this);
        init();
    }

    private void init() {
        vBox = new VBox();
        vBox.setPrefWidth(100);
        panelVeci = new FlowPane();
        Label label = new Label("Panel batohu");
        vBox.getChildren().addAll(label, panelVeci);
        aktualizuj();
    }

    public VBox getPanel() {
        return vBox;
    }

    @Override
    public void aktualizuj() {
        panelVeci.getChildren().clear();
        String nazevObrazku;
        for (String jmenoVeci : batoh.getSeznamVeci().keySet()){
            nazevObrazku = "/zdroje/"+ jmenoVeci + ".png";
            ImageView vecImageView = new ImageView(new Image
                    (Tajemny_ostrov_Adventura.class.getResourceAsStream(nazevObrazku),
                            100, 100, false, false));
            panelVeci.getChildren().add(vecImageView);
        }
    }
}

Hra

package main;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import logika.Hra;
import logika.IHra;
import uiText.TextoveRozhrani;

import java.awt.*;

public class Tajemny_ostrov_Adventura extends Application {

    private BorderPane borderPane;
    private TextArea centerTextArea;
    private IHra hra;
    private FlowPane dolniFlowPane;
    private Label zadejPrikazLabel;
    private TextField prikazTextField;
    private OknoProstoru oknoProstoru;
    private PanelVychodu panelVychodu;
    private PanelBatohu panelBatohu;

    public static void main(String[] args) {
        if (args.length == 0) {
            launch(args);
        } else {
            if (args[0].equals("-text")) {
                IHra hra = new Hra();
                TextoveRozhrani ui = new TextoveRozhrani(hra);
                ui.hraj();
            } else {
                System.out.println("Neplatny parametr");
            }
        }

    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent e) {
                Platform.exit();
                System.exit(0);
            }
        });

        borderPane = new BorderPane();
        hra = new Hra();
        nastavTextArea();
        borderPane.setCenter(centerTextArea);
        nastavDolniPanel();
        borderPane.setBottom(dolniFlowPane);

        oknoProstoru = new OknoProstoru(hra.getHerniPlan());
        borderPane.setTop(oknoProstoru.getPanel());

        panelVychodu = new PanelVychodu(hra.getHerniPlan());
        borderPane.setRight(panelVychodu.getList());

        panelBatohu = new PanelBatohu(hra.getBatoh());
        borderPane.setLeft(panelBatohu.getPanel());

        Scene scene = new Scene(borderPane, 900, 650);
        primaryStage.setTitle("Tajemný ostrov");
        primaryStage.setScene(scene);
        prikazTextField.requestFocus();
        primaryStage.show();

    }

    private void nastavTextArea() {
        centerTextArea = new TextArea();
        centerTextArea.setText(hra.vratUvitani());
        centerTextArea.setEditable(false);
    }

    private void nastavDolniPanel () {
        dolniFlowPane = new FlowPane();
        zadejPrikazLabel = new Label("Zadej příkaz: ");
        zadejPrikazLabel.setFont(Font.font("TimesRoman", FontWeight.BOLD, 20));
        prikazTextField = new TextField();
        dolniFlowPane.setAlignment(Pos.CENTER);
        dolniFlowPane.getChildren().addAll (zadejPrikazLabel, prikazTextField );

        prikazTextField.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String radek = prikazTextField.getText();
                String text = hra.zpracujPrikaz(radek);
                centerTextArea.appendText("\n" + radek + "\n");
                centerTextArea.appendText("\n" + text + "\n");
                prikazTextField.setText("");
                if (hra.konecHry()) {
                    prikazTextField.setEditable(false);

                }
        };

    });
}


}