CharlesWoodchuck Posted February 12, 2020 Share Posted February 12, 2020 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(); } Quote Link to comment Share on other sites More sharing options...
Camaro Posted February 13, 2020 Share Posted February 13, 2020 Could do public void onStart() throws InterruptedException { gui = new fighterGUI(); while (gui.isVisible()) { sleep(1000); } . . . } to make it wait 1 Quote Link to comment Share on other sites More sharing options...
CharlesWoodchuck Posted February 14, 2020 Author Share Posted February 14, 2020 @camaro 09 Thanks. I ended up adding an open() method that sets the GUI as visible. Since it's a dialog it requires an answer, so I open() the GUI and it doesn't run through anything else until the GUI has been closed. Fixing the problem Quote Link to comment Share on other sites More sharing options...