java Applet中线程“AWT

系统教程 行业动态 更新时间:2024-06-14 16:57:17
java Applet中线程“AWT-EventQueue-1”中的异常java.lang.NullPointerException(Exception in thread “AWT-EventQueue-1” java.lang.NullPointerException in java Applet)

我在java中创建一个程序,它从applet读取文本文件,并将颜色放在由文件定义的applet窗口像素中。 问题是当我运行这个程序时,Exception发生了,我已经完成了我所知道的所有事情来解决它但没有成功。

我的applet代码是

GUIIO.java

import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class GUIIO extends Applet { Color color = new Color(2); InputStream inputStream; BufferedReader bufferedReader; @Override public void init() { try { inputStream = new FileInputStream("Sample In.txt"); } catch (Exception e) { e.printStackTrace(); } } @Override public void paint(Graphics g) { Point point = new Point(); try { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); point = this.getImageResolution(bufferedReader); char c, ch[] = {'a', 'b', 'c', 'd', 'e', 'f' }; int i = 0, x = 0, y = 0; putPixel(1, 100, String.valueOf(ch), g); while((c = (char)bufferedReader.read()) != 'z') { if(c == 'y') { y++; x = 0; } else if(i < 6) { ch[i] = c; i++; } if (i == 6) { putPixel(x, y, String.valueOf(ch), g); x++; i = 0; ch[i] = c; } } } catch (IOException e) { e.printStackTrace(); } g.drawString(point.x+" "+point.y, 30, 10); repaint(); } private Point getImageResolution(BufferedReader bufferedReader) throws IOException { boolean xFlag = false; StringBuilder x = new StringBuilder(), y = new StringBuilder(); String check = null; char chars[] = bufferedReader.readLine().toCharArray(); for(int i=0;i<chars.length;i++) { check = String.valueOf(chars[i]); if(check.equals("x")) xFlag = true; else if(xFlag == true) y.append(check); else x.append(check); } Point point = new Point(); point.x = Integer.parseInt(x.toString()); point.y = Integer.parseInt(y.toString()); return point; } private void putPixel(int x, int y, String color, Graphics g) { g.setColor(Color.decode("0x"+color)); g.drawLine(x, y, x, y); } }

输入文本文件。

样本In.txt

8x8 000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffyz

我在eclipse中遇到的异常或错误

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at GUIIO.getImageResolution(GUIIO.java:61) at GUIIO.paint(GUIIO.java:30) at sun.awt.RepaintArea.paintComponent(Unknown Source) at sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

更新:我已经在cmd中尝试了这个代码它运行得很完美但是当我调整窗口大小时我会给出同样的错误。

请帮忙。

I am creating a program in java which reads a text file from applet and puts colors in applet window pixels defined by file. The problem is that when i run this program the Exception occurs and i have done everything i know to resolve it but didn't succeed.

My applet code is

GUIIO.java

import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class GUIIO extends Applet { Color color = new Color(2); InputStream inputStream; BufferedReader bufferedReader; @Override public void init() { try { inputStream = new FileInputStream("Sample In.txt"); } catch (Exception e) { e.printStackTrace(); } } @Override public void paint(Graphics g) { Point point = new Point(); try { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); point = this.getImageResolution(bufferedReader); char c, ch[] = {'a', 'b', 'c', 'd', 'e', 'f' }; int i = 0, x = 0, y = 0; putPixel(1, 100, String.valueOf(ch), g); while((c = (char)bufferedReader.read()) != 'z') { if(c == 'y') { y++; x = 0; } else if(i < 6) { ch[i] = c; i++; } if (i == 6) { putPixel(x, y, String.valueOf(ch), g); x++; i = 0; ch[i] = c; } } } catch (IOException e) { e.printStackTrace(); } g.drawString(point.x+" "+point.y, 30, 10); repaint(); } private Point getImageResolution(BufferedReader bufferedReader) throws IOException { boolean xFlag = false; StringBuilder x = new StringBuilder(), y = new StringBuilder(); String check = null; char chars[] = bufferedReader.readLine().toCharArray(); for(int i=0;i<chars.length;i++) { check = String.valueOf(chars[i]); if(check.equals("x")) xFlag = true; else if(xFlag == true) y.append(check); else x.append(check); } Point point = new Point(); point.x = Integer.parseInt(x.toString()); point.y = Integer.parseInt(y.toString()); return point; } private void putPixel(int x, int y, String color, Graphics g) { g.setColor(Color.decode("0x"+color)); g.drawLine(x, y, x, y); } }

The input text file.

Sample In.txt

8x8 000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffyz

The exception or error i have got in eclipse

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at GUIIO.getImageResolution(GUIIO.java:61) at GUIIO.paint(GUIIO.java:30) at sun.awt.RepaintArea.paintComponent(Unknown Source) at sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

Update: I have tried this code in cmd it runs perfectly but when i resized the window i gives same error.

Please help.

最满意答案

您的代码中存在多个问题。 首先,您无法读取和读取bufferedReader,因为在某些时候它将为空,并且您不会检查从bufferedReader读取的行是否为空。

还有另一个问题,有时你的颜色字符串是?????? 。

为什么每次重新绘制apllet时都会读取文件? 您可以在init()方法中读取一次文件,就是这样!

There are muliple issues in your Code. First you can't read and read and read from your bufferedReader because at sometime it will be empty and you don't check if the line that you read from the bufferedReader is null.

There is also another problem, sometimes your color String is ?????? .

Why do you read the file every time the apllet is repainted? You could read the file once in your init() methode and that's it!

更多推荐

本文发布于:2023-04-12 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e147cd94604409497fe2d89536e99eb9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   java   Applet   AWT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!