Where are you initializing the `_myFObj` object? If the root exception cause is `_myFObj.loadDB();` throwing an exception, then it's pretty clear that `_myFObj` is NULL.

Looks like you haven't read the whole NPE message properly - it's the very first line it mentions that you need to look at, the rest of the lines it refers to are just how it got there.

Where are you initializing the `_myFObj` object? If the root exception cause is `_myFObj.loadDB();` throwing an exception, then it's pretty clear that `_myFObj` is NULL.

exactly, I don't know why is it null. all I need is to call loadDB() present in myFunctions class from myUI class. The only way I can think of doing is to create an object of myFunctions class inside myUI class and call the loadDB() method.

but I dont understand why the object is null.


Another alternative I could think of is by placing the loadDB() in the constructor of myFunctions() and by calling the constructor from myUI()

class myFunctions()
{
     public myFunctions()
     {
          loadDB();
          // other code
     }
}
class UI
{
     public myUI()
     {
          // other code
          new myFunctions();
     }
}

but I dont think this is of any good to me.
------

also when I removed the loadDB() and _myFOBj from myUI, the application runs fine!
but all my action listening is written in myFunctions how can I again assign the listening and not create any errors?


how do i fix this issue?

You still haven't answered the question "Where are you initializing the `_myFObj` object"?

my 'myUI' class is the main frame of my application. It needs to get input from user and process the input and give out result in form of images.

I have separated the UI and logic in two separate classes called myUI class (which contains UI) and myFunctions class (which contain logic).

the 1st thing my application does when the myUI loads up is read a text file and store it in the memory somewhere so the password can be used to verify user input. so the logic for fetching text file, reading it and storing passwords is written in myFunctions class.

and when I compile and run the program, the myLoader class starts up and creates a frame and loads up the UI (by calling myUI class) but in order to load up the text file and read password I need to call up the loadDB method present in myFunctions class right?

So THATS why I initialized an object of myFunctions class so as to call up the function in my myUI class.


------

have you understood now?

Yeah yeah yeah, we get all that. But "Where are you initializing the `_myFObj` object"?
In other words: where exactly do you have the following syntax fragment:
_myFObj =

exactly, I don't know why is it null

A reference variable is always null until you assign a value to it

ah my bad, didn't read your question properly. I didnt have the myFunctions object initialized!

I have added it in myUI class and the NPE vanished! thanks!

myUI.java

class myUI extends JFrame
{
     myFunctions myFObj = new myFunctions();
     public myUI
     {
           // blah blah...
     }
}

myFunctions.java

class myFunctions implements ActionListener
{
     myUI myUIObj;   -------  ???????

     public myUI
     {
           // blah blah...
          loadDB();
     }

     public loadDB()
     {
           // blah blah   ----- WORKS FINE!
     }
     public void actionPerformed(ActionEvent e)
     {
    	if(e.getSource() == myUIObj._button[0])
    	{
           // blah blah
        }
     }	
}

Im facing a new problem now.. when my Frame's up and running and when I click a button, it gives me an nullpointer error again! at

(e.getSource() == myUIObj._button[0])

and then When I try to Initialize the myUIObj it gives me a stackoverflow error.


how do I fix this now?

this is what I get when Iry to compile the app.

Exception in thread "main" java.lang.StackOverflowError
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:244)
    at javax.swing.UIDefaults.getUI(UIDefaults.java:751)
    at javax.swing.UIManager.getUI(UIManager.java:989)
    at javax.swing.JPanel.updateUI(JPanel.java:109)
    at javax.swing.JPanel.<init>(JPanel.java:69)
    at javax.swing.JPanel.<init>(JPanel.java:92)
    at javax.swing.JPanel.<init>(JPanel.java:100)
    at uniKEY.myUI.<init>(myUI.java:37)
    at uniKEY.myFunctions.<init>(myFunctions.java:30)

and the last 2 lines

at uniKEY.myUI.<init>(myUI.java:37)
    at uniKEY.myFunctions.<init>(myFunctions.java:30)

repeat continuously for 10-15 times or more.

myUI.java

public myUI()
    {                //  <--------------------- Line # 102

myFunctions.java

class myFunctions implements ActionListener
{
	myUI myUIObj = new myUI();    //  <--------------------- Line # 30
class myUI extends JPanel
{
	myFunctions _myFObj = new myFunctions();    //  <--------------------- Line # 37

these are the errors which I am having.. how do I eliminate the errors?

Your class 'myUI' creates a new 'myFunctions' object and your class 'myFuntions' creates a new 'myUI' object resulting in a never ending loop and hence the program running out of stack space. You need to decide which one comes first, 'myUI' or 'myFunctions' although logically it should be the UI class which gets created first which in turn instantiates the helper 'myFunctions' class object.

ah I get it, I have eliminated the loop now.
and yes, that sequence of loading is my idea too. i.e., start myLoader then myUI then myFunctions.

but the action listener class is in myFunctions class which needs to have access to variables of myUI class.

how do I create a reference of myUI class in myFunctions so that I can use the buttons (which belong to myUI)??
how do I set a reference of myUI class in myFunctions class.

Sounds like you haven't got the allocation of functions to classes quite right. An ActionListener that accesses buttons should be in the GUI class. A "MyFunctions" kind of class should be reusable from various front-ends (Swing GUI, browser interface etc), and thus can't contain any hard-coded references to a particular front-end.

Typically, in the GUI ActionListener you would collect user input from the form then pass it as parameters to the fuctions object, which returns one or more values that the GUI can then display. So the GUI class handles all the form-based GUI stuff, and the functions class just does the logic/claculations etc.

Your case here is a little bit special, in that the underlying functional model is a model of a user interface, but this still works if you keep the model abstract in terms of how the user interactions are actually displayed.

Finally - you may need to use a Listener ("observer pattern") to handle GUI changes that are initiated by the functions layer (eg changing state after a delay). This is also very common when this kind of architecture is done right (Swing itself is a reallly good example!).

ok, one LAST question (I Hope)

I am coding this program in JCreator, and when I compile and run the program using JCreator, the program runs fine. but when I manually go to the directory and try running the application, I get a ClassNotFoundException.

c:\jcreator>java myLoader
Exception in thread "main" java.lang.NoClassDefFoundError: myLoader
Caused by: java.lang.ClassNotFoundException: myLoader
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: myLoader.  Program will exit.

I can see that the classes have been neatly stacked in a folder called uniKEY.

and when I navigate to the classes folder (uniKEY folder), and try running the program with same command I get this error

c:\jcreator>java myLoader
Exception in thread "main" java.lang.NoClassDefFoundError: myLoader (wrong name:
 uniKEY/myLoader)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: myLoader.  Program will exit.

how do I run this manually from command line? I want to make a batch file for the application to start up.

thanks.

You always require the fully qualified class name when referring to your class. Try something like:

c:\jcreator>java uniKEY.myLoader

ah I see, Thanks for the help!
also I need to use the timer provided in java.util instead of the javax.swing one...

I was wondering if this is the right way to do it?

public java.util.Timer _myTimer;
_myTimer.schedule(new java.util.TimerTask (){public void run(){resetState();}},5000);

that code doesn't work.. :/

this is what Im trying to do. I'm rewriting the following code by using a java.util Timer!

_myTimer = new Timer(5000,new ActionListener()
		{ 
			public void actionPerformed(ActionEvent e)
			{
				resetState();
			}
		}
		); 

.......
......
 public void resetState()
{
        _myTimer.stop();

}

what mistake am I doing here?

That's how you use a swing timer. java.util.timer is different. Check its schedule(...) methods.
ps: Why not use a swing timer?

Hi James, im doing this project as a part of my assignment. and it requires my application to be three-tiered and must be using java.util.Timer

how do I make this application three-tiered? I have tried separating the actionlistener into a new class and have failed miserably.. any pointers which way I should follow now?

OK, java,util.timer it is then. Just remember that as well as creating the timer you have to schedule actions to make anythiung happen.

As for 3-tier. To me this means user interface, business logic/object model, persistence/database layer. I don't see how this applies to your project, which seems to have just the top 2 layers. Maybe he means MVC (model/view/controller) - in which case I have to bow out here because I've never found it to make any sense to separate the GUI elements from the GUI logic. I guess you'll have to check what your prof means by 3 tier.

how do I schedule the timer? I've tried once but I couldn't do it right :(

public java.util.Timer _myTimer;
_myTimer.schedule(new java.util.TimerTask (){public void run(){resetState();}},5000);

--


also, I was wondering if it was possible to an instance of a class?


for example:

class x
{

     public x v;
     public z w;

     public x()
     {
        // blah
        w.show();
     }

     public x getInstance()
     {
           return v; 
     }
}

class y
{
     public x a;
     public y()
     {
        a = new x();
     }
     public static void main(String[] args)
     {
         new y();
     }
}

class z
{
     public x b;
     public z()
     {
        b = new x();
     }
     public void show()
     {
          System.out.println(b.getInstance());
     }
}

can this be done?

in simple words: I have 3 classes, 1st class has object of 2nd class. I need to access variables of 2nd class in 3rd class.. how is it possible?

how do I schedule the timer? I've tried once but I couldn't do it right :(

public java.util.Timer _myTimer;
_myTimer.schedule(new java.util.TimerTask (){public void run(){resetState();}},5000);

--


also, I was wondering if it was possible to an instance of a class?


for example:

class x
{

     public x v;
     public z w;

     public x()
     {
        // blah
        w.show();
     }

     public x getInstance()
     {
           return v; 
     }
}

class y
{
     public x a;
     public y()
     {
        a = new x();
     }
     public static void main(String[] args)
     {
         new y();
     }
}

class z
{
     public x b;
     public z()
     {
        b = new x();
     }
     public void show()
     {
          System.out.println(b.getInstance());
     }
}

can this be done?

in simple words: I have 3 classes, 1st class has object of 2nd class. I need to access variables of 2nd class in 3rd class.. how is it possible?

1. You need to create a new Timer before you try to schedule something with it.
2. Can't read you sample code - please stick to the Java convention of starting all class names with a capital letter - it really helps.
3. In general you pass the relevant instance of 2nd class to the 3rd class that needs it (eg in 3rd class' constructor)

i used this code:

class myClass
{
        _myLapTimer = new Timer();
	_myLapTimer.schedule(new myLapTimer(),5000);
	
	class myLapTimer extends TimerTask
	{
		public void run()
		{
			resetState();
		}
	}
        public void reset()
        {
                cancelTimer();
	}

	public void cancelTimer()
	{
		_myLapTimer.cancel();	
	}
        
        public void check()
        {
             if(true)
             {
  		_myLapTimer.schedule(new myLapTimer(),5000);
             }
             else
             {
  		_myLapTimer.schedule(new myLapTimer(),5000);
             }
        }
}

kis this the right way to do it?

It looks OK, tho I haven't tried to complile or run it. Have you?
I'm a bit baffled by the if test:
if (true) dosomething
else doexactlythesamething

Sorry for being a total jackass and abandoning this thread.

big massive thanks and apologies to JamesCherrill, ~s.o.s~, and NormR1 for all your help and support during the grim period of my life.

Thanks to you guys, now I have a masters degree and a Jr. Developer position in a reputed firm UK. I cannot express how grateful I am to you guys for all your support. if you are around london/reading/maidenhead give me a shout, I'll definitely buy you guys a pint or a dozen.

AND FINALLY:

uniKEY.zip

Here is the final product free to use for whatever you may wish to use it for.

cheers mates!

commented: :) +17

That's good news. Well done!
J

commented: :D +2

It really makes me happy and gives a warm fuzzy feeling knowing that the small amount of guidance provided helped you in the long run. Good luck with your new job! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.