Jump to content

Issue with GUI & NPE


CharlesWoodchuck

Recommended Posts

Hi,

I'm currently working on a fighting script with a gui, and when I try to start the script im getting a NPE before I even select my options on my gui.

Does anyone know why this is happening? I'm getting the NPE on the 1st if() statement if(gui.getMonster().equalsIgnoreCase("chicken")) because getMonster hasnt been answered yet.

public void onStart() throws InterruptedException {
    gui = new fighterGUI();
    if(gui.getMonster().equalsIgnoreCase("chicken")){
        targetMonsterName = gui.getMonster();
        log(targetMonsterName);
        //targetMonsterArea = new Area(pos1, pos2);
    }
    if(gui.getMonster().equalsIgnoreCase("chaos druid")){
        targetMonsterName = "Chaos druid";
        log(targetMonsterName);
        //targetMonsterArea = new Area(pos1, pos2);
    }
    if(gui.getFood().equalsIgnoreCase("salmon")){
        selectedFood = "Salmon";
        log(selectedFood);
    }
    if(gui.getFood().equalsIgnoreCase("lobster")){
        selectedFood = gui.getFood();
        log(selectedFood);
    }

 

class fighterGUI extends JDialog implements ActionListener {
    private final JLabel monsterLabel;
    private final JLabel foodLabel;
    private final JComboBox monsterDropDown;
    private final JComboBox foodDropDown;
    private final JButton button;
    private int monsterIndex;
    private int foodIndex;
    private String monsterName;
    private String foodName;
    private String[] monsterList;
    private String[] foodList;
    private boolean started;

    public fighterGUI() {
        monsterList = new String[]{"Chicken", "Chaos druid"}; //{"Chicken", "Cow", "Goblin", "Hill giant", "Chaos druid"};
        foodList = new String[]{"Salmon", "Lobster"};
        setTitle("Vain's Low Level Combat Fighter");
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
        setSize(600, 100);
        setModal(true);
        setLayout(new FlowLayout());
        monsterLabel = new JLabel("Select Monster");
        monsterDropDown = new JComboBox(monsterList);
        foodLabel = new JLabel("Select Food");
        foodDropDown = new JComboBox(foodList);
        button = new JButton("Start Script");
        //button.addActionListener(this);
        button.addActionListener(e -> {
                    started = true;
                    close();
        });
        add(monsterLabel);
        add(monsterDropDown);
        add(foodLabel);
        add(foodDropDown);
        add(button);
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //monsterIndex = monsterDropDown.getSelectedIndex();
        setMonster((String) monsterDropDown.getSelectedItem());
        //foodIndex = foodDropDown.getSelectedIndex();
        setFood((String) foodDropDown.getSelectedItem());
        //dispose();
    }

    public void setMonster(String monster) {
        this.monsterName = monster;
    }
    public String getMonster(){
        return monsterName;
    }
    public void setFood(String food){
        this.foodName = food;
    }
    public String getFood(){
        return foodName;
    }
    public boolean isStarted() {
        return started;
    }
    public void open() {
        setVisible(true);
    }
    public void close() {
        setVisible(false);
        dispose();
    }
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...