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.