2011-11-09 40 views
1

當我在構造函數中調用我創建的方法draw()時,我總是收到一個NullPointerException異常。這特別令人沮喪,因爲我找到了解決方法,但這不是我想要的。這是可行的代碼。 公共類TutorialGrid擴展的JFrame {當試圖在桌子上繪製圖形時,爲什麼會出現NullPointerException?

private JPanel contentPane; 
private Graphics g; 
private int currentlength; 
private int currentwidth; 
private Integer[][] maze = new Integer[20][30]; 
private static TutorialGrid frame; 
public static JTable table; 
private JTextField title; 
/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       frame = new TutorialGrid(); 
       frame.setVisible(true); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public TutorialGrid(){ 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(0, 0, 1366, 768); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    ImageIcon icon = new ImageIcon("C:\\Users\\Brendan\\Desktop\\GAME\\Images\\BG7.jpg"); 
    input(); 


    table = new JTable(); 
    table.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      draw(maze); 
     } 
    }); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    table.setAutoCreateRowSorter(true); 
    table.setCellSelectionEnabled(true); 
    table.setColumnSelectionAllowed(true); 
    table.setDoubleBuffered(true); 
    table.setDragEnabled(true); 
    table.setFillsViewportHeight(true); 
    table.setFocusCycleRoot(true); 
    table.setFocusTraversalPolicyProvider(true); 
    table.setIgnoreRepaint(true); 
    table.setInheritsPopupMenu(true); 
    table.setSurrendersFocusOnKeystroke(true); 
    table.setBackground(new Color(0, 0, 0)); 
    table.setForeground(new Color(255, 255, 255)); 
    table.setBorder(new LineBorder(new Color(139, 0, 0))); 
    table.setBounds(180, 40, 1000, 600); 
    contentPane.add(table); 

    title = new JTextField(); 
    title.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      draw(maze); 
     } 
    }); 
    title.setHorizontalAlignment(SwingConstants.CENTER); 
    title.setText("Click to Start"); 
    title.setBorder(new LineBorder(new Color(0, 128, 0))); 
    title.setEditable(false); 
    title.setForeground(Color.WHITE); 
    title.setBackground(new Color(0, 0, 0)); 
    title.setBounds(606, 11, 151, 20); 
    contentPane.add(title); 
    title.setColumns(10); 
    JLabel lblBgpanel = new JLabel("", icon,JLabel.CENTER); 
    lblBgpanel.setBounds(0, 0, 1360, 740); 
    contentPane.add(lblBgpanel); 
} 

正如你可以看到我有在桌子上,這在所有調用draw方法成功沒有問題的標題都mouseListeners。它在桌子上繪製我想要的網格,但是爲了繪製它,我必須單擊其中一個容器。我希望它在JFrame初始化時繪製網格。但如果我簡單地畫(迷宮);在構造函數中它給了我一個空指針異常。這是用於繪製網格的繪製和輸入方法的代碼。

 public void draw(Integer[][] maze){ 
    int x= 125; 
    int y =50; 
    int width1 =25; 
    int length1 =25; 
    g=table.getGraphics(); 
    for(int i=0; i<20; i++) 
     { 

     for(int j=0; j<30; j++) 
     { 

      if(maze[i][j] == maze[currentlength][currentwidth]) 
      { 
       g.setColor(Color.YELLOW); 
       g.fillRect(x,y,width1,length1); 
       g.setColor(Color.RED); 
       g.drawRect(x,y,width1,length1); 
       x = x+25; 
      } 
      else if(maze[i][j] == 1) 
      { 
        g.setColor(Color.BLACK); 
        g.fillRect(x,y,width1,length1); 
        g.setColor(Color.RED); 
        g.drawRect(x,y,width1,length1); 
        x = x+25; 
      } 

       else if(maze[i][j] == 0) 
       { 
        g.setColor(Color.BLUE); 
        g.fillRect(x,y,width1,length1); 
        g.setColor(Color.RED); 
        g.drawRect(x,y,width1,length1); 
        x = x+25; 
       } 
       else if(maze[i][j] == -2) 
       { 
        g.setColor(Color.GREEN); 
        g.fillRect(x,y,width1,length1); 
        g.setColor(Color.RED); 
        g.drawRect(x,y,width1,length1); 
        x = x+25; 
       } 
        else if(maze[i][j] == -10) 
        { 
         g.setColor(Color.WHITE); 
         g.fillRect(x,y,width1,length1); 
         g.setColor(Color.RED); 
         g.drawRect(x,y,width1,length1); 
         x = x+25; 
        } 
     } 
     y=y+25; 
     x=125; 
     } 
} 

     public void input(){ 
    //Imports and reads grid file 
     Scanner scan = null; 

     try 
     { 

      FileReader grid = new FileReader("C:\\Users\\Brendan\\Desktop\\tutorialgrid.txt"); 
      scan = new Scanner(grid); 


     } 
     catch(FileNotFoundException e) 
     { 
      System.out.println(e.getMessage() + "Could not find that file"); 
      System.exit(0); 
     } 

     for(int i=0; i<20; i++) 
     { 
     for(int j=0; j<30; j++) 
     { 
      maze[i][j]=scan.nextInt(); 
      if(maze[i][j] == -1) 
      { 
       currentlength = i; 
       currentwidth = j; 
      } 
      if(maze[i][j] == -10) 
      { 
      } 
     } 
     } 
} 

}

這一切都是同一個類中。這是我想要做的,但給我一個錯誤。我在構造函數的底部添加了draw(迷宮),當我嘗試運行它時,它會炸燬我。

public TutorialGrid(){ 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(0, 0, 1366, 768); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    ImageIcon icon = new ImageIcon("C:\\Users\\Brendan\\Desktop\\GAME\\Images\\BG7.jpg"); 
    input(); 


    table = new JTable(); 
    table.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      draw(maze); 
     } 
    }); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    table.setAutoCreateRowSorter(true); 
    table.setCellSelectionEnabled(true); 
    table.setColumnSelectionAllowed(true); 
    table.setDoubleBuffered(true); 
    table.setDragEnabled(true); 
    table.setFillsViewportHeight(true); 
    table.setFocusCycleRoot(true); 
    table.setFocusTraversalPolicyProvider(true); 
    table.setIgnoreRepaint(true); 
    table.setInheritsPopupMenu(true); 
    table.setSurrendersFocusOnKeystroke(true); 
    table.setBackground(new Color(0, 0, 0)); 
    table.setForeground(new Color(255, 255, 255)); 
    table.setBorder(new LineBorder(new Color(139, 0, 0))); 
    table.setBounds(180, 40, 1000, 600); 
    contentPane.add(table); 

    title = new JTextField(); 
    title.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      draw(maze); 
     } 
    }); 
    title.setHorizontalAlignment(SwingConstants.CENTER); 
    title.setText("Click to Start"); 
    title.setBorder(new LineBorder(new Color(0, 128, 0))); 
    title.setEditable(false); 
    title.setForeground(Color.WHITE); 
    title.setBackground(new Color(0, 0, 0)); 
    title.setBounds(606, 11, 151, 20); 
    contentPane.add(title); 
    title.setColumns(10); 
    JLabel lblBgpanel = new JLabel("", icon,JLabel.CENTER); 
    lblBgpanel.setBounds(0, 0, 1360, 740); 
    contentPane.add(lblBgpanel); 
    draw(maze); 
} 

這是錯誤。

java.lang.NullPointerException 
at game.TutorialGrid.draw(TutorialGrid.java:136) 
at game.TutorialGrid.<init>(TutorialGrid.java:111) 
at game.TutorialGrid$1.run(TutorialGrid.java:41) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

game.TutorialGrid.draw第136行是draw方法開始的行。 Game.TutorialGrid。第111行是構造函數中的最後一行,我把draw(迷宮)放在了這裏。 game.TutorialGrid $ 1.run第41行是線框= new TutorialGrid();

幫助表示讚賞。

+0

你不應該在構造函數中工作。只需初始化對象並出去。在完全構造完成後調用一個方法來完成任何繪圖。 –

回答

0

getGraphics()返回null,直到事物可見。

+0

非常感謝,讓它工作。 – Frosty

+1

不,不推薦使用getGraphics()(調整窗口大小以查看原因)。請參閱教程以獲取自定義繪畫的正確方法。 http://download.oracle.com/javase/tutorial/uiswing/painting/ –

相關問題