Results 1 to 3 of 3
- 04-30-2008, 01:15 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 1
- Rep Power
- 0
Null pointer Exception. after a bit of execution!! Plz help me
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.Timer;
import javax.swing.Timer;
class CA_1D2S3N {
private byte[][]rows;
private int curRow=0,nextRow=1;
private int rowSize=50;
private int cellStates=2;
private byte[] rules={0,1,1,1,1,0,0,0};
public CA_1D2S3N(){}
public CA_1D2S3N(int rSize){
rowSize=rSize;
rows=new byte[2][];
rows[curRow] =new byte[rowSize];
rows[nextRow] =new byte[rowSize];
reset();
}
public int getRowSize(){
return rowSize;
}
public void reset(){
for(int i=curRow;i<=nextRow;++i){
for(int j=0;j<rowSize;j++){
rows[i][j]=0;
}
}
rows[curRow][(int)(rowSize/2)]=1;
}
public void TimeStep(){
for(int i=1;i<rowSize-2;++i){
int state=Map(rows[curRow],i);
rows[1][i]=rules[state];
}
for(int i=0;i<rowSize;i++){
rows[0][i]=rows[1][i];
}
}
int Map(byte []row,int i){
int x=4*row[i-1] + 2*row[i] + 1*row[i+1];
return x;
}
byte[] getcurrentRow(){
return rows[curRow].clone();
}
}
public class MainFrame extends JApplet implements ActionListener{
public JPanel pane;
CA_1D2S3N ca=new CA_1D2S3N (50);
int timeStep=101;
public JMenuBar menuBar;
public JToolBar toolBar;
JButton Tstart;
JButton Tstop;
public static Timer timer=new Timer(100,null);
public void timerEvent(ActionEvent e){
ca.TimeStep();
ShowCurrentRow(ca.getcurrentRow(),++timeStep);
if (timeStep>100) timer.stop();
}
public MainFrame() {
menuBar = new JMenuBar();
JMenu formatMenu = new JMenu("Timer");
formatMenu.add("Start");
formatMenu.add("Stop");
menuBar.add(formatMenu);
Tstart=new JButton("Start");
Tstop=new JButton("Stop");
toolBar = new JToolBar("Formatting");
toolBar.add("Start",Tstart);
Tstart.addActionListener(this);
formatMenu.addActionListener(this);
timer.addActionListener(new Main_Frame_Timer_ActionAdapter(this));
toolBar.addSeparator();
}
public void start() {
MainFrame example = new MainFrame();
example.pane = new JPanel();
example.pane.setPreferredSize(new Dimension(500, 500));
example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
example.toolBar.setMaximumSize(example.toolBar.get Size());
JFrame frame = new JFrame("CA Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setJMenuBar(example.menuBar);
frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
frame.getContentPane().add(example.pane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public void stop() {
}
public void destroy() {
}
public void clear(){
Graphics g=pane.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, pane.getWidth(), pane.getHeight());
}
public void ShowCurrentRow(byte[] row,int time){
Graphics g=pane.getGraphics();
for(int i=0;i<row.length;++i){
if(row[i]>0)
g.fillRect(3*(10+i), 3*(time+5), 3, 3);
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==Tstart){
if(timer.isRunning()) timer.stop();
else{
if(timeStep>100){
ca.reset();
timeStep=0;
clear();
ShowCurrentRow(ca.getcurrentRow(),timeStep);
}
timer.start();
}
}
}
}
class Main_Frame_Timer_ActionAdapter implements ActionListener{
MainFrame adaptee;
public Main_Frame_Timer_ActionAdapter(MainFrame ad){
this.adaptee=ad;
}
public void actionPerformed(ActionEvent e) {
// TODO add timer event
adaptee.timerEvent(e);
}
}
//-----------Error----------
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at MainFrame.ShowCurrentRow(MainFrame.java:166)
at MainFrame.timerEvent(MainFrame.java:108)
at Main_Frame_Timer_ActionAdapter.actionPerformed(Mai nFrame.java:207)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 04-30-2008, 09:12 PM #2
Note what happens to your graphics after the applet is minimized/restored or partially un/covered. This happens when you draw inside event code. To avoidJava Code:// <applet code="MFApplet" width="500" height="500"></applet> // run this at the prompt with: >appletviewer MFApplet.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.border.BevelBorder; public class MFApplet extends JApplet implements ActionListener{ public JPanel pane; CA_1D2S3N ca=new CA_1D2S3N (50); int timeStep=101; public JMenuBar menuBar; public JToolBar toolBar; JButton Tstart; JButton Tstop; public static Timer timer=new Timer(100,null); public void timerEvent(ActionEvent e){ ca.TimeStep(); ShowCurrentRow(ca.getcurrentRow(),++timeStep); if (timeStep>100) timer.stop(); } // public MFApplet() { // this is a constructor for an app public void init() { // applet construction code goes here menuBar = new JMenuBar(); JMenu formatMenu = new JMenu("Timer"); formatMenu.add("Start"); formatMenu.add("Stop"); menuBar.add(formatMenu); Tstart=new JButton("Start"); Tstop=new JButton("Stop"); toolBar = new JToolBar("Formatting"); toolBar.add("Start",Tstart); Tstart.addActionListener(this); formatMenu.addActionListener(this); timer.addActionListener(new Main_Frame_Timer_ActionAdapter(this)); toolBar.addSeparator(); pane = new JPanel(); pane.setPreferredSize(new Dimension(500, 500)); pane.setBorder(new BevelBorder(BevelBorder.LOWERED)); Dimension size = toolBar.getSize(); Dimension prefSize = toolBar.getPreferredSize(); System.out.printf("toolBar size = [%2d, %2d]%n" + " prefSize = [%2d, %2d]%n", size.width, size.height, prefSize.width, prefSize.height); toolBar.setMaximumSize(toolBar.getSize()); setJMenuBar(menuBar); getContentPane().add(toolBar, BorderLayout.NORTH); getContentPane().add(pane, BorderLayout.CENTER); } public void start() { // This method is called everytime an applet is restored. // To run this applet as an application move the code that // was here off to a main method. } public void stop() {} public void destroy() {} public void clear(){ Graphics g=pane.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, pane.getWidth(), pane.getHeight()); } public void ShowCurrentRow(byte[] row,int time){ Graphics g=pane.getGraphics(); for(int i=0;i<row.length;++i){ if(row[i]>0) g.fillRect(3*(10+i), 3*(time+5), 3, 3); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==Tstart){ if(timer.isRunning()) timer.stop(); else{ if(timeStep>100){ ca.reset(); timeStep=0; clear(); ShowCurrentRow(ca.getcurrentRow(),timeStep); } timer.start(); } } } /** Convenience main to run applet as an aplication. */ public static void main(String[] args) { MFApplet applet = new MFApplet(); Dimension d = applet.getPreferredSize(); System.out.printf("applet prefSize = [%d, %d]%n", d.width, d.height); applet.setPreferredSize(new Dimension(500, 500)); JFrame frame = new JFrame("CA Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.pack(); applet.init(); frame.setVisible(true); } } class Main_Frame_Timer_ActionAdapter implements ActionListener{ MFApplet adaptee; public Main_Frame_Timer_ActionAdapter(MFApplet ad){ this.adaptee=ad; } public void actionPerformed(ActionEvent e) { // TODO add timer event adaptee.timerEvent(e); } } class CA_1D2S3N { private byte[][]rows; private int curRow=0,nextRow=1; private int rowSize=50; private int cellStates=2; private byte[] rules={0,1,1,1,1,0,0,0}; public CA_1D2S3N(){} public CA_1D2S3N(int rSize){ rowSize=rSize; rows=new byte[2][]; rows[curRow] =new byte[rowSize]; rows[nextRow] =new byte[rowSize]; reset(); } public int getRowSize(){ return rowSize; } public void reset(){ for(int i=curRow;i<=nextRow;++i){ for(int j=0;j<rowSize;j++){ rows[i][j]=0; } } rows[curRow][(int)(rowSize/2)]=1; } public void TimeStep(){ for(int i=1;i<rowSize-2;++i){ int state=Map(rows[curRow],i); rows[1][i]=rules[state]; } for(int i=0;i<rowSize;i++){ rows[0][i]=rows[1][i]; } } int Map(byte []row,int i){ int x=4*row[i-1] + 2*row[i] + 1*row[i+1]; return x; } byte[] getcurrentRow(){ return rows[curRow].clone(); } }
this do your drawing inside the JPanel "pane"s paintComponent method.
In java pseudocode:
Then you alter/control/set the state/member variables in this class from your applet event code and tell it to repaint.Java Code:... public void init() { ... pane = new Pseudo(); ... class Pseudo extends JPanel { // Use booleans or other member variables to control // the state of this component. Set up the drawing // code inside [i]paintComponent[/i] so it can/will // draw the current state of this compoinent at any time. /** override this method for custom drawing */ protected void paintComponent(Graphics g) { super.paintComponent(g); // do custom drawing here. } }
- 05-01-2008, 10:14 AM #3
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null pointer exception error
By brownie_jedi in forum New To JavaReplies: 3Last Post: 03-15-2008, 06:27 AM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM -
statement null pointer exception
By bbq in forum JDBCReplies: 1Last Post: 07-05-2007, 04:23 AM -
Execution cut
By Eric in forum Advanced JavaReplies: 1Last Post: 06-27-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
tags around your code! It stops errors and makes it much easier to read.
me! :cool:
Bookmarks