1

This is one of the projects I started last year with BlueJ, but ran into problems and just didnt finish it. Now I've come back to it, and really need help.

This is my GUI class:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 * 
 * @author TANVEER AHMED
 */
public class HouseAdmin
{
    private JFrame frame = new JFrame();
    private JMenuBar menuBar  = new JMenuBar();
    private JMenu fileMenu = new JMenu("Menu");
    private JMenuItem listAllAppliances;
    private JMenuItem turnOnSingleAppliance;
    private JMenuItem switchOnAllLights;
    private BorderEventListener listener;//this class the inner class to handle the events
    private JTextArea textArea;
    private String printHouse;
    private House myHome;


    /**
     * Constructor for objects of class HouseAdmin
     */
    public HouseAdmin()
    {

        menuBar = new JMenuBar();

       listAllAppliances = new JMenuItem("List All appliances...");
       turnOnSingleAppliance = new JMenuItem("Turn on a single appliance...");
       switchOnAllLights = new JMenuItem("Switch On All Lights...");

       fileMenu.add(listAllAppliances);
       fileMenu.add(turnOnSingleAppliance);
       fileMenu.add(switchOnAllLights);

       menuBar.add(fileMenu);


       textArea = new JTextArea(30,70);
       frame.add(textArea, BorderLayout.CENTER);

       frame.setJMenuBar(menuBar);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible (true);
       frame.pack();

       listener = new BorderEventListener();
       listAllAppliances.addActionListener(listener);
       turnOnSingleAppliance.addActionListener(listener);
       switchOnAllLights.addActionListener(listener);
      setupHouse();



    }



    private void setupHouse(){
        //Sets up a house
        House myHome = new House("Hatfield");

        Light bulb1 = new Light(1,"Bed Light",50,"H");
        Light bulb2 = new Light(2,"Kitchen Light",5,"L");
        Light bulb3 = new Light(3,"Garden Light",30,"E");
        Light bulb4 = new Light(4,"Bathroom Light",50,"H");
        Light bulb5 = new Light(5,"Shower Light",5,"L");
        Light bulb6 = new Light(6,"Solar Light",30,"E");
        Light bulb7 = new Light(7,"Passage Light",5,"L");
        Light bulb8 = new Light(8,"Storage Light",50,"H");
        Light bulb9 = new Light(9,"Garage Light",30,"H");
        Light bulb10 = new Light(10,"Living room Light",30,"E");

        myHome.addAppliance(bulb1);
        myHome.addAppliance(bulb2);
        myHome.addAppliance(bulb3);
        myHome.addAppliance(bulb4);
        myHome.addAppliance(bulb5);
        myHome.addAppliance(bulb6);
        myHome.addAppliance(bulb7);
        myHome.addAppliance(bulb8);
        myHome.addAppliance(bulb9);
        myHome.addAppliance(bulb10);


        printHouse = myHome.toString();

        //textArea.setText(myHome.toString());


    }


    class BorderEventListener implements ActionListener{

        public void actionPerformed (ActionEvent e){

             if(e.getSource() == listAllAppliances){
                textArea.setText(printHouse);

           }

            if (e.getSource () == turnOnSingleAppliance){
                String code  = JOptionPane.showInputDialog("Please Enter Application Code");
                int code2 = Integer.parseInt(code);

                myHome.turnOnASingleAppliance(code2);
                textArea.append("Appliance " + code2 + " Has been turned on!");
                textArea.setText(printHouse);

            }

            if (e.getSource () == switchOnAllLights){
                myHome.turnAllLightsOn();

                textArea.setText(printHouse);
                textArea.append("All lights have been turned on!");

            }

        }
    }

}

http://pastebin.com/dgLwaHSF

You can see I call a method called "turnOnSingleAppliance" on line 105. This now crashes and throws the terminal window at me.

This is the code for the method:

import java.util.*;
/**
 *
 * @author TANVEER AHMED
 */
public class House {
 private String address;
 private int powerConsumption;
 private ArrayList <ElectricalAppliance> HouseAppliances;
 public House(String address) {
  // initialise instance variables
  this.address = address;
  HouseAppliances = new ArrayList <ElectricalAppliance>();

 }


public ElectricalAppliance turnOnASingleAppliance(int code){
        for (int i = 0; i < HouseAppliances.size(); i++){
            ElectricalAppliance app = HouseAppliances.get(i);
            if (app.getCode() == code ){
                app.lightSwitchOn();
            }
        }
        return null;
    }
}

http://pastebin.com/8krvvVHW

perhaps you guys need to see more source? if so I can upload the whole bluej project.

My gut is telling me the loop is wrong. What do you guys think?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
tanner
  • 35
  • 1
  • 5
  • 2
    Please share your code and the stacktrace of the exception here – Mureinik Jan 09 '16 at 19:20
  • 1
    Please post pertinent code, preferably a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve), here with your question, not in a link. Links can go dead, and links may hold large programs, programs too large to ask a volunteer to review. Your compliance with this request will be greatly appreciated and will likely help you in getting better and faster help. – Hovercraft Full Of Eels Jan 09 '16 at 19:20
  • 1
    I've edited your question and have included your code, however in the future, please do this yourself. – Hovercraft Full Of Eels Jan 09 '16 at 19:22
  • 1
    Next -- what do you mean by "crash"? Please tell and show the details. – Hovercraft Full Of Eels Jan 09 '16 at 19:23
  • Here is the error guys: http://pastebin.com/Ds37bEWj Sorry I dont know what to upload specifically as theres so much source but I believe this is the main part which need attention – tanner Jan 09 '16 at 19:23
  • @HovercraftFullOfEels Thanks for embedding the code, Im a novice programmer and new guy here I wasn't sure how to display all the code on my post. Sorry this is the error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at HouseAdmin$BorderEventListener.actionPerformed(HouseAdmin.java:109) – tanner Jan 09 '16 at 19:24
  • OK, so after all that, you've got a NullPointerException -- the key information has finally been reported. You must do what we tell everyone to do with this type of bug: **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Jan 09 '16 at 19:26
  • @HovercraftFullOfEels sorry I was a bit confused on what to do, I understand you want it all on the main post now. Will do this now, few mins please. – tanner Jan 09 '16 at 19:26
  • @tanner: don't bother. the question has been closed as a duplicate NPE question, and you must debug it yourself. Please read the link to the duplicate question/answer [here](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it), and my comment above. This is a common problem and thankfully the solution is easy, but only you can do it since only you have runnable code. A debugger might help you as well. – Hovercraft Full Of Eels Jan 09 '16 at 19:28
  • Please take a look at the [tour], the [help] section as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) section, so that your future experiences here will be better. – Hovercraft Full Of Eels Jan 09 '16 at 19:29
  • Thanks for working with us to improve your question by the way, it is much appreciated. – Hovercraft Full Of Eels Jan 09 '16 at 19:37

1 Answers1

1

Change this line

House myHome = new House("Hatfield");

To this

myHome = new House("Hatfield");

You are not defining the field in the first instance, and so your error is likely at this line inside the event listener.

myHome.turnOnASingleAppliance(code2);

What you have done is a simple mistake and is called "variable shadowing", meaning you are defining a local variable by the same name as a field.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Just wanted to say thank you. This has resolved my problem. I cant believe its something so simple, I have been stressing about this problem for ages and it just made no sense as to why I couldn't do what I was trying. I really am grateful for your help, so thank you once again. I am also going to research "variable shadowing" so I can prevent this in the future. – tanner Jan 09 '16 at 20:18