DaveDavidson
DaveDavidson

Reputation: 206

Exception in thread "main" java.land.NullPointerException

I know there are a lot of posts 'similar' to this on stack overflow, however I do understand this is from accessing/modifying something which has not been assigned. However I cannot see anything like this inside my main:

private static AuctionClient frame = null;;
//public AuctionServer auction = new AuctionServer();
private JTextField lineToSend;
private JComboBox itemsList;
private JLabel label;
private JTextArea received;
private JButton closeConnection, sendBid;
private static Socket socket;
private Scanner input;
private PrintWriter output;
private static InetAddress host; 
private static Scanner networkInput;

public static void main(String[] args) throws IOException
{
    AuctionClient frame = new AuctionClient();
    frame.setSize(600,400);
    frame.setVisible(true);


            InetAddress host = null;
            final int PORT = 1234;
            Socket socket;
            Scanner networkInput,keyboard;
            PrintWriter output;

            try
            {
                host = InetAddress.getLocalHost();
            }
            catch(UnknownHostException uhEx)
            {
                System.out.println("\nHost ID not found!\n");
            }

            socket = new Socket(host, PORT);
            networkInput = new Scanner(socket.getInputStream());
            output = new PrintWriter(
                                socket.getOutputStream(),true);

}


public AuctionClient() throws IOException
    {


    String[] itemName = {"Item1","Item2"};
    JPanel itemPanel,entryPanel;
    JLabel itemPrompt,messagePrompt;

    setLayout(new FlowLayout());
    itemsList = new JComboBox();
    itemsList.addItem(itemName);

    ComboHandler handler = new ComboHandler();
    itemsList.addItemListener(handler);
    add(itemsList);
    //label = new JLabel(auction.itemName[0]);
    //Start up showing image1.
    add(label);


    entryPanel = new JPanel();

    messagePrompt = new JLabel("Enter Bid Amount");
    lineToSend = new JTextField(15);
    lineToSend.setEditable(true);
    lineToSend.addActionListener(this);
    entryPanel.add(messagePrompt);
    entryPanel.add(lineToSend);
    add(entryPanel,BorderLayout.WEST);

    received = new JTextArea(10,15);
    received.setEditable(false);
    add(new JScrollPane(received), BorderLayout.EAST);

    closeConnection = new JButton("Close connection");
    sendBid = new JButton("Enter bid");
    sendBid.addActionListener(this);
    closeConnection.addActionListener(this);
    add(closeConnection, BorderLayout.SOUTH);
    add(sendBid, BorderLayout.SOUTH);
 }

Complete error below:

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1091)
    at java.awt.Container.add(Container.java:1003)
    at javax.swing.JFrame.addImpl(JFrame.java:567)
    at java.awt.Container.add(Container.java:415)
    at AuctionClient.<init>(AuctionClient.java:72)
    at AuctionClient.main(AuctionClient.java:28)

Upvotes: 0

Views: 623

Answers (1)

Asoub
Asoub

Reputation: 2371

Your problem is on the line add(label); at line 72, you can see this by the stacktrace at AuctionClient.<init>(AuctionClient.java:72) because label is null.

Upvotes: 3

Related Questions