gpt4 book ai didi

java - 为什么尝试在表格上绘制图形时会出现 NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 05:36:51 25 4
gpt4 key购买 nike

当我在构造函数中调用我创建的方法 draw() 时,我不断收到 NullPointerException。这尤其令人沮丧,因为我找到了解决方法,但这不是我想要的。这是有效的代码。 公共(public)类 TutorialGrid 扩展 JFrame{

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

正如你所看到的,我在表格和标题上都有 mouseListeners,它们成功地调用了绘制方法,没有任何问题。它在 table 上绘制了我想要的网格,但是为了绘制它,我必须单击这些容器之一。我希望它在 JFrame 初始化时绘制网格。但如果我简单地把draw(maze);在构造函数中它给了我一个空指针异常。这是用于绘制网格的绘制和输入方法的代码。

        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行是draw方法开始的行。游戏教程网格。第 111 行是构造函数中的最后一行,我在其中放置了绘制(迷宫)。game.TutorialGrid$1.run第41行是线frame = new TutorialGrid();

感谢帮助。

最佳答案

getGraphics() 返回 null,直到事物可见为止。

关于java - 为什么尝试在表格上绘制图形时会出现 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com