problem in thread when calling a class member's method



Hello

I have a problem when runing a thread. I have a gui application with a
menu and a menu item. The application extends JPanel and implements
ActionListener. It has several members and one specific member called
lets say "arrow1" which causes he problem. This member is an inner
class instance of a class called "ClassToCall". This class has a method
called "foo" which does nothing but write something to Sytem.out. This
arrow1.foo is called from inside run(). I get the following:

Exception in thread "Thread-2" java.lang.NullPointerException
at MyProblem$InnerThread.run(MyProblem.java:157)
at java.lang.Thread.run(Unknown Source)


In order to replicate the problem try to build the code I have below
and when gui begins click Tranformations -> tranformationA and you get
the problem.

My major problem is I can't figure out why this is hapening and where
to start from

Any directions or ideas?


Thank you!


The code of the app is stripped to the bare minimun.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MyProblem extends JPanel implements ActionListener
{
private ClassToCall arrow1;

/////// start GUI stuff
private JFrame mainFrame;

private JMenuBar menuBar;

private JMenu fileMenu;
private JMenuItem quitItem;
private JMenuItem againItem;

private JMenu transformationMenu;
private JMenuItem tranformationAItem;

private int horizontialDimension;
private int verticalDimension;

/////// end GUI stuff

private int index;

private boolean tranformationA;

Graphics display;
Font font;
InnerThread in;
Thread tr;

public MyProblem()
{

///////////////// start set up the gui
setSize(600,400);

mainFrame = new JFrame("SwingTest");
mainFrame.setSize(600, 400);

// create a menu bar
menuBar = new JMenuBar();

/* create "File" menu fileMenu */
fileMenu = new JMenu("File");
quitItem = new JMenuItem("Quit");
againItem = new JMenuItem("Do it Again");

quitItem.addActionListener(this);
againItem.addActionListener(this);

fileMenu.add(quitItem);
fileMenu.add(againItem);

menuBar.add(fileMenu);

/* create "Tranformations" menu tranforma */
transformationMenu = new JMenu("Transformations");
tranformationAItem = new JMenuItem("Tranformation A");

tranformationAItem.addActionListener(this);

transformationMenu.add(tranformationAItem);

menuBar.add(transformationMenu);

/* Put menuBar, Panel to the application's frame */
mainFrame.getContentPane().add(menuBar, BorderLayout.NORTH);
mainFrame.getContentPane().add(this, BorderLayout.CENTER);

horizontialDimension = getSize().width;
verticalDimension = getSize().height;

mainFrame.setVisible(true);

display = getGraphics();
font = new Font("TimesRoman", Font.BOLD, 36);

tranformationA = false;
////////////////////ending set up the gui

ClassToCall arrow1 = new ClassToCall();
}



public void paint(Graphics g)
{
if(tranformationA)
{
g.drawLine(50, 50, 50, 100);
}
index++;
}



public void update(Graphics g)
{
g.setColor(getBackground());
g.fillRect(0, 0, horizontialDimension, verticalDimension);
g.setColor(getForeground());
paint(g);
}



public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==quitItem)
{
System.exit(0);
}
else if(ae.getSource()== tranformationAItem)
{

tranformationA = true;
in = new InnerThread();
tr = new Thread(in);
tr.start();
}
}



public static void main(String[] args)
{
MyProblem mp = new MyProblem();
}


public class InnerThread implements Runnable
{
public void run()
{
while (index < 50)
{
//!!!!!!!!!!!!!!!!!!!! THE PROBLEM IS CAUSED HERE !!!!!!!!!!!!
arrow1.foo();
}
index = 0;
System.out.println("Thread finished");
}
}



public class ClassToCall
{
public ClassToCall()
{
}

public void foo()
{
System.out.println("do arrow.foo()");
}
}

}

.



Relevant Pages

  • Re: Problem mit BindingSource
    ... Public Class Form36 ... Private Sub Form36_Load(ByVal sender As System.Object, ... private MeinDTO _backup; ... public void BeginEdit() ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • Re: Can we over-load "+" in Java?
    ... public class Person { ... private String sName; ... private Person leader; ... public void setTeamLeader ...
    (comp.lang.java.programmer)
  • Re: Can we over-load "+" in Java?
    ... public class Person { ... private String sName; ... private Person leader; ... public void setTeamLeader ...
    (comp.lang.java.programmer)
  • Re: Capturing both stdout and stderr of exec-ed process in JTextArea
    ... private static int returnValue = Integer.MIN_VALUE; ... public JtaCommandRunner(JTextArea jta) { ... public void run{ ... public class JtaOutReader implements Runnable { ...
    (comp.lang.java.gui)
  • hibernate - what is wrong??
    ... public class Bean1 { ... private long bean1id; ... private Bean2 bean2; ... public void setBean1id{ ...
    (comp.lang.java.databases)