Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 41
Registered: ‎04-24-2012
My Device: storm
My Carrier: blackberry

Blackberry java typecasting question

Hi Im trying to port a andriod application in Blackberry java but the problem is Ithat some parts of my code does not behave right. I'm trying to typecast a object and check if its a instanceof the class but in blackberry java It seems to the value of the type casted object is null. In andriod the typecasted object does not give me a null. The only difference is that the andriod jre that im using is java 1.7 and in blackberry the default jre is java1.3. Is there any explaination on why It behaved this way? Is it a complier or runtime issue in java 1.3? I'm really kinda of lost on where the problem is located. Below is the example a sample on what I have done the get this problem it is in the doStuff method in the TestMain class. Any Comments and Suggestions are very appreciated Tnx in advance.

 

public class TestTask implements Test
{
    Test tryTest = null;
    
    public static final TestTask create(Test test)  {
        TestTask v = new eq.gui.TestTask();
        v.tryTest = test;
        return(v);
    }
}

public class TestHandler
{
    public void handlerTask(Test test) {
        if(test == null) {
            return;
        }
        TestTask tt = TestTask.create(test);
        if(main != null) {
            main.doStuff(((Test)tt));
        }
    }
}

public class TestMain
{
    public void doStuff(Test test) {
        Test tempTest = null;
        TestTask tt = ((TestTask)((tempTest = test) instanceof TestTask ? tempTest : null));
        if(tt == null) {
            System.out.println("Null");
        }
        else {
            System.out.println("NotNull");    
        }
    }
}

 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: Blackberry java typecasting question

I don't see why there should be any difference in the implememtation.  But stranger things have happened..

 

Anyway I wonder about this code:

        Test tempTest = null;
        TestTask tt = ((TestTask)((tempTest = test) instanceof TestTask ? tempTest : null));

 

This has potential to confuse the reader, as well as the compiler.  Can you not code it more simply as:

 

TestTask tt = null;

if ( test instanceof TestTask ) {

ii = (TestTask) test;

}

 

or

 

TestTask tt = (test instanceof TestTask ? ((TestTask) test) : null));

Contributor
Posts: 41
Registered: ‎04-24-2012
My Device: storm
My Carrier: blackberry

Re: Blackberry java typecasting question

well I can't just change the implementation without any proof on why it behaves like that. Is there any way to use a higher version of a compiler to test if there is a problem with the code? because the only significant difference is that blackberry java compiler is using java 1.3/1.4. I'm using the Blackberry plugin for eclipse companent pack 7.0 by the way. 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: Blackberry java typecasting question

"well I can't just change the implementation without any proof on why it behaves like that"

 

I don't understand why not, but anyway, you could test to see if the changed code has the same problem.  if so, you are looking at a compiler issue. 

 

I have ported an Android application, and there are going to be plenty of places where you have to change the source, all the generics for one.  So don't be scared of changing the Android code.