problem of null pointer exception!! plz help me!! When i run!!
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   rohan_chauhan
Posted On:   Wednesday, April 30, 2008 07:08 AM

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;    More>>

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
rows[i][j]=0;
}
}
rows[curRow][(int)(rowSize/2)]=1;


}
public void TimeStep(){
for(int i=1;i
int state=Map(rows[curRow],i);

rows[1][i]=rules[state];
}
for(int i=0;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.getSize());

JFrame frame = new JFrame("CA Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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
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 is

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(MainFrame.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.pumpOneEventForHierarchy(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)

   <<Less

Re: problem of null pointer exception!! plz help me!! When i run!!

Posted By:   Stephen_McConnell  
Posted On:   Wednesday, April 30, 2008 11:33 AM

WHY doesn't anyone read the posting rules here. Please, look at your post and see if YOU could read it, much less anyone wanting to help you.


Stephen McConnell

About | Sitemap | Contact