AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem in thread when calling a class member's method

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
oleth
Guest





PostPosted: Fri Apr 28, 2006 10:12 pm    Post subject: problem in thread when calling a class member's method Reply with quote



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()");
}
}

}
Back to top
A. Bolmarcich
Guest





PostPosted: Sat Apr 29, 2006 12:12 am    Post subject: Re: problem in thread when calling a class member's method Reply with quote



On 2006-04-28, oleth <oleth5962 (AT) yahoo (DOT) gr> wrote:
Quote:
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?

The arrow1 variable used by the run method is null because a value
is never assigned to it. The arrow1 variable used by the the run
method is the private instance variable of the class MyProblem.

The program contains the statement

ClassToCall arrow1 = new ClassToCall();

at the end of the MyProblem constructor. However, that declares and
initalizes an arrow1 variable that is local to that constructor.
Chances are you want to replace that statement with

arrow1 = new ClassToCall();

which assigns to the arrow1 variable the arrow1 variable used in the
run method.
Back to top
oleth
Guest





PostPosted: Sat Apr 29, 2006 11:12 am    Post subject: Re: problem in thread when calling a class member's method Reply with quote



You are absolutely right. That's the problem. With
ClassToCall arrow1 = new ClassToCall();
I initialize a local, to the constructor, variable.

you can hear my head going bang bang on the screen :-)

Thanks Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.