2017-08-25 26 views
0

IDE告訴我我有一個 線程25和38線程「主要」java.lang.NullPointerException異常 所有線路38應該做的是設置面板的邊界以包含遊戲的標題,當我改變屏幕時,我將稍後擺脫它。 25行基本上將代碼放入主要方法中。有人請幫助我java給我例外線程「main」java.lang.NullPointerException

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.*; 

import static java.awt.Color.black; 
import static java.awt.Color.white; 

public class Game { 
    //GUI variables 
    JFrame window; 
    Container container; 
    JPanel titleNamePanel, startButtonPanel, mainTextPanel; 
    JLabel titleNameLabel; 
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 98); 
    Font startButtonFont = new Font("Times New Roman", Font.PLAIN, 50); 
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 28); 
    JButton startButton; 
    JTextArea mainTextArea; 
    TitleScreenHandler TSHandler = new TitleScreenHandler(); 
    ChoiceHandler choiceHandler = new ChoiceHandler(); 

    public static void main(String[]args){ 
     new Game(); 
    } 
    public Game(){ 

     //window 
     window = new JFrame(); 
     window.setSize(800, 800); 
     window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     window.getContentPane().setBackground(black); 
     window.setLayout(null); 
     window.setTitle("ADVENTURE CAVE"); 

     //Title Name 
     titleNamePanel.setBounds(100, 20, 600, 200); 
     titleNamePanel.setBackground(black); 
     titleNameLabel = new JLabel("ADVENTURE CAVE"); 
     titleNameLabel.setForeground(white); 

     //Start button 
     startButtonPanel = new JPanel(); 
     startButtonPanel.setBounds(300, 270, 250, 120); 
     startButtonPanel.setBackground(black); 
     startButtonPanel.setFont(startButtonFont); 
     startButton.addActionListener(TSHandler); 
     startButton.setFocusPainted(false); 

     window.setVisible(true); 

    } 
    public void gotoMainGameScreen(){ 
     titleNamePanel.setVisible(false); 
     startButtonPanel.setVisible(false); 

     //Main Panel 
     mainTextPanel = new JPanel(); 
     mainTextPanel.setBounds(100, 100, 600, 250); 
     mainTextPanel.setBackground(black); 
     container.add(mainTextPanel); 

     //Main Text 
     mainTextArea = new JTextArea("You have reached the Cave of Adventures;"); 
     mainTextArea.setBounds(100, 100, 600, 250); 
     mainTextArea.setBackground(black); 
     mainTextArea.setForeground(white); 
     mainTextArea.setFont(normalFont); 
     mainTextArea.setLineWrap(true); 
     mainTextPanel.add(mainTextArea); 

    } 
    public class TitleScreenHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e){ 
      gotoMainGameScreen(); 
     } 
    } 
    public class ChoiceHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent event){ 

     } 
    } 

} 
+1

'titleNamePanel'永遠不會初始化之前您的通話setBounds Vivick

回答

0

你試圖在titleNamePanel尚未初始化,但(所以目前默認設置爲null)再做一次手術。

你需要做的:

titleNamePanel = new JPanel(); 

...只是

相關問題