participate


Java Programming [Archive] - Wierd problem
<<   Back to Forum  |   Give us Feedback
4 Duke Stars available
This topic has 6 replies on 1 page.
imNirmalya
Posts:23
Registered: 1/22/03
Wierd problem   
Apr 17, 2003 1:28 AM

 
Hi All,

I've been facing a very wierd problem.
I have two classes, A & B under the same package XYZ.

A has a method, a(); and B has a method b(), which calls a().
A is a singleton class.

What's wierd is that for some unknown reason I can't get to the current instance of A, from B's b() and get an Exception which shouldn't even be occuring (since those parts of a() aren't reached during execution)!
And when I can the same bit of code from A's main method, it works!

This is what I mean.. here's how the code looks like:
public class A
{ private static A instance;
  
  //Returns the current instance of this class
    public static A getInstance()
    { if(instance==null)
        instance = new A();  
      return instance;
    }
 
  public void a()
  { System.out.println ("Inside a()");
    //..
    //Some operations..
    //..then..
      if(someVeryRareCondition)
        throw new ExceptionA(reasonOfExceptionString) 
  }//end-of-a()-method
 
  public static void Main(String[] args)
  { A.getInstance().a(); //<--This works, prints the String "Inside a()"
  }
}//end-of-class A
 
public class B
{ public b() 
  { A.getInstance().a(); //<--This does NOT work, get the ExceptionA
  }
}//end-of-classB


Infact, even if b() looked like :
public b() 
  { A objA = A.getInstance(); //--(pt.A)
    objA .a(); //--(pt.B)
  }

Before it reached (pt.A), I'm getting the above mentioned Exception..
There's no other class where such an Exception is ever thrown or caught.
[ExceptionA belongs to the same package XYZ].

Any comments would be greatly appreciated.
Thanks in advance.

Nirmalya
 
Abelhinha
Posts:470
Registered: 6/11/02
Re: Wierd problem   
Apr 17, 2003 1:46 AM (reply 1 of 6)  (In reply to original post )

 
Are you really sure it's impossible for this exception to occurr?
Cause after all, you are calling method a() from b(), and since you didn't tell what happens inside, it's impossible to judge whether this error is really unreachable.
By the code you gave, I cannot reproduce the error.
(btw This won't compile, but I suppose that's just the example)
 
imNirmalya
Posts:23
Registered: 1/22/03
Re: Wierd problem   
Apr 17, 2003 2:07 AM (reply 2 of 6)  (In reply to #1 )

 
Thanks for the reply..
Here's a brief description of what I'm trying to do:
I am writing a plugin for Eclipse. Class A calls some code in some packages in some external JAR files (no problems faced in using those JAR files).

I stepped thru the debugger. In the code,
public b() 
  { A objA = A.getInstance(); //--(pt.A)
    objA .a(); //--(pt.B)
  }
I wasn't even able to reach A's getInstance() method at pt.A. So its really very wierd for Eclipse to give me that exception when I'm just trying to get A's instance (at pt.A) which does not even call those 3rd party packages. Moreover to ensure that my "someVeryRareCondition" doesn't somehow become true by magic, i inserted a dummy println stmt as soon as the method is entered... currently even this prinltn stmt is not reached!

Any clue? Yes I know its very wierd... we've been trying to figure it out ourselves for almsot day now... Anyways thanks for your time reading thru this.

Regards,
Nirmalya
 
Abelhinha
Posts:470
Registered: 6/11/02
Re: Wierd problem      
Apr 17, 2003 2:16 AM (reply 3 of 6)  (In reply to #2 )

 
It's maybe a silly suggestion, but aren't you by any chance getting the exception from somewhere else (dunno, another thread, in another method that also throws this exception?) Cause I really can't see how you'd get an exception from a()while you're not even entering this method...
 
imNirmalya
Posts:23
Registered: 1/22/03
Re: Wierd problem   
Apr 17, 2003 2:31 AM (reply 4 of 6)  (In reply to #3 )

 
Initially .. we did suspect something similar .. but we later reconfirmed that wasn't the case as its a single-threaded application.

I'll post this question on the eclipse.tools newsgroup and see what they have to say on this. Thanks for taking time to read thru this wierd wierd problem..

Regards,
Nirmalya
 
man35
Posts:166
Registered: 7/8/02
Re: Wierd problem   
Apr 17, 2003 2:32 AM (reply 5 of 6)  (In reply to #3 )

 
I've just tried this very quickly.

//----File A.java-----
package tmp;

public class A
{
private static A instance;

public static A getInstance()
{
if(instance==null) instance = new A();
return instance;
}

public void a()
{
System.out.println("Inside a");
}

public static void main(String args[])
{
A.getInstance().a();
B objB = new B();
objB.b();
}
}

//----File B.java-----
package tmp;

public class B
{
public void b()
{
System.out.println("Inside b");
A.getInstance().a();
}
}



Worked fine. Without any problems as it should.
I think there is some other condition generating exception and this is not the cause of your exeception.

-Manish.
 
levi_h
Posts:4,012
Registered: 2/24/01
Re: Wierd problem   
Apr 17, 2003 2:33 AM (reply 6 of 6)  (In reply to #2 )

 
And it isn't in the A () constructor?
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums