Results 1 to 3 of 3
  1. #1
    rohan is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default 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)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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();
        }
    }
    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 avoid
    this do your drawing inside the JPanel "pane"s paintComponent method.
    In java pseudocode:
    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.
        }
    }
    Then you alter/control/set the state/member variables in this class from your applet event code and tell it to repaint.

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hello rohan,

    Welcome to the Java Forums.

    In future, please put the tags around your code! It stops errors and makes it much easier to read.
    Attached Thumbnails Attached Thumbnails Null pointer Exception. after a bit of execution!! Plz help me-codetags2.bmp  
    Did this post help you? Please me! :cool:

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM
  3. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  4. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 05:23 AM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •