2016-03-08 86 views
0

我知道在堆棧溢出中有很多帖子「類似」,但我明白這是通過訪問/修改尚未分配的東西。但是我看不出這樣的事情在我的主:線程「main」中的異常java.land.NullPointerException

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); 
} 

完整的錯誤如下:

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) 
+0

請發佈完整的堆棧跟蹤。 –

+0

爲您推薦。 – DaveDavidson

+0

這是完整的堆棧跟蹤? –

回答

3

你的問題就行了add(label);在管線72,可以通過堆棧跟蹤看到這個at AuctionClient.<init>(AuctionClient.java:72)因爲label一片空白。

相關問題