생성자에서 만든 메서드, 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);
}
당신은 내가 테이블과 전혀 성공적으로 추첨 방식에는 문제를 호출하지 제목 모두에있는 MouseListener를 볼 수 있듯이. 테이블에 원하는 그리드를 그립니다. 그리기 위해서는 그 컨테이너 중 하나를 클릭해야합니다. 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)
{
}
}
}
}
은} 같은 클래스 안에 있습니다. 이것은 내가하려고하는 것이지만 나에게 오류를 준다. 생성자 맨 아래에 무승부 (미로)를 추가하면 실행하려고하는 순간 나에게 불어납니다.
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)는 연신 방법이 시작되는 라인이다. game.TutorialGrid. 111 행은 내가 draw (maze)를 두는 생성자의 마지막 줄입니다. game.TutorialGrid $ 1.run line 41은 라인 프레임 = 새로운 TutorialGrid()입니다.
도움을 주시면 감사하겠습니다.
생성자에서 작업하지 않아야합니다. 그냥 개체를 초기화하고 나가. 모든 드로잉을 수행하기 위해 완전히 생성 된 후에 메소드를 호출하십시오. –