Skip to main content

java.lang.NullPointerException - I cannot find why

7 replies [Last post]
peter_budo
Offline
Joined: 2006-10-18

I'm little experimenting with FileConnection.
So far I put together this code

package browser;</p>
<p>import javax.microedition.lcdui.Display;<br />
import javax.microedition.lcdui.Displayable;<br />
import javax.microedition.midlet.MIDlet;</p>
<p>public class MobileFileBrowser extends MIDlet{</p>
<p>    private Display display = Display.getDisplay(this);<br />
    public MobileFileBrowser(){}</p>
<p>    public void startApp(){<br />
        changeScreen(CoreUI.FILE_CONN_DETECTION);<br />
    }</p>
<p>    public void pauseApp(){}</p>
<p>    public void destroyApp(boolean unconditional){}</p>
<p>    public void quitApp(){<br />
        destroyApp(true);<br />
        notifyDestroyed();<br />
    }</p>
<p>    /**<br />
     * Method to call classes as declared in CoreUI interface<br />
     * @param _uiClass represent the name of the class that need to invoked<br />
     */<br />
    public void changeScreen(String _uiClass){<br />
        System.out.println("Change screen with string "+ _uiClass);<br />
        Displayable displayable;<br />
        try{<br />
            displayable = (Displayable)Class.forName(_uiClass).newInstance();<br />
            display.setCurrent(displayable);<br />
            ((CoreUI)displayable).setUIManager(this);<br />
        }<br />
        catch(Exception e){<br />
            e.printStackTrace();<br />
        }<br />
    }</p>
<p>    /**<br />
     * Method provide way to display Alerts on the screen<br />
     * @param d represent displayable (alert) to be presented to user<br />
     */<br />
    /*public void changeScreen(Displayable d){<br />
        System.out.println("Change screen with displayable");<br />
        display.setCurrent(d);<br />
        ((CoreUI)d).setUIManager(this);<br />
    } */<br />
}

<br />
package browser;</p>
<p>public interface CoreUI {</p>
<p>    public final String FILE_CONN_DETECTION = "browser.gui.FileConnDetection";<br />
    public final String FILE_BROWSER = "browser.gui.FileBrowser";</p>
<p>    void setUIManager(MobileFileBrowser _mgr);<br />
}

<br />
package browser.gui;</p>
<p>import javax.microedition.lcdui.Alert;<br />
import javax.microedition.lcdui.AlertType;<br />
import javax.microedition.lcdui.Form;</p>
<p>import browser.MobileFileBrowser;<br />
import browser.CoreUI;</p>
<p>public class FileConnDetection extends Form implements CoreUI{</p>
<p>    private MobileFileBrowser mgr;</p>
<p>    public FileConnDetection(){<br />
        super("File Connection Detection");</p>
<p>        if(System.getProperty(<br />
                "microedition.io.file.FileConnection.version") != null){<br />
            System.out.println("FileConnection API found calling "+CoreUI.FILE_BROWSER);<br />
            mgr.changeScreen(CoreUI.FILE_BROWSER);<br />
        }<br />
        else{<br />
            System.out.println("FileConnection API NOT found");<br />
            StringBuffer deviceData = new StringBuffer(mgr.getAppProperty("MIDlet-Name"))<br />
                    .append("\n").append(mgr.getAppProperty("MIDlet-Vendor"))<br />
                    .append("\nFileConnection API is not available");<br />
            Alert alert = new Alert("Missing API", deviceData.toString(), null, AlertType.INFO);<br />
            alert.setTimeout(Alert.FOREVER);<br />
           /* Display.getDisplay(mgr).setCurrent(alert);<br />
            mgr.destroyApp(true);*/<br />
        }<br />
    }</p>
<p>    public void setUIManager(MobileFileBrowser _mgr){<br />
        mgr = _mgr;<br />
    }<br />
}

<br />
package browser.gui;</p>
<p>import javax.microedition.lcdui.Form;</p>
<p>import browser.MobileFileBrowser;<br />
import browser.CoreUI;</p>
<p>public class FileBrowser extends Form implements CoreUI{</p>
<p>    private MobileFileBrowser mgr;</p>
<p>    public FileBrowser(){<br />
        super("File Browser");<br />
    }</p>
<p>    public void setUIManager(MobileFileBrowser _mgr){<br />
        mgr = _mgr;<br />
    }<br />
}

Change screen with string browser.gui.FileConnDetection
FileConnection API found calling browser.gui.FileBrowser
java.lang.NullPointerException
at browser.gui.FileConnDetection.(+28)
at java.lang.Class.runCustomCode(+0)
at browser.MobileFileBrowser.changeScreen(+32)
at browser.MobileFileBrowser.startApp(+6)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

you are referencing mgr in the FileConnDetection constructor, but you do not pass it in, and the setUIManager is an instance method so mgr could not be set when you are trying to call a method on it resulting in NullPointerException.

-Shawn

peter_budo
Offline
Joined: 2006-10-18

Then I'm messed up big time. The above code is based on the advice you give me in this thread http://forums.java.net/jive/thread.jspa?threadID=61985&tstart=0 and based on this concept I built the other application and it is working that is funny part about it.

Can you please show me the changes as they should be made? It seem obvious now that I may misinterpreted recommendations made by you

Typo
Message was edited by: peter_budo

sfitzjava
Offline
Joined: 2003-06-15

You are not too far off base. Although I think the FileConnDetection is not something that should be a UI container per se.

All you need to do is move your constructor logic into the setUIManager() method.

peter_budo
Offline
Joined: 2006-10-18

Yes, FileConnDetection it is not exactly UI container but I wanted to play it safe as I'm not sure how Alert would be handled if library not found. So therefore I made it based upon the Form.

I moved that if/else statement to separated method that is now called from setUIManager(). However it does feel somewhat weird. Is there any other (better) way?

sfitzjava
Offline
Joined: 2003-06-15

For another way (maybe better maybe not), the FileConnDetection, could be some initialization logic in your startApp that test for the library, then passes the class name to load if successful, else have an overloaded version of changeScreen to take an Alert item. This gives you a way to pop an alert onto the screen from any screen if needed, since all screens are going to have access to the UI Manager.

To keep action logic out of the setUIManager() you can add a method to your CoreUI interface to support for a generalized action message receiver. Something like "boolean onAction(int _action)", then have a switch/case that routes to specialized methods.

This is where JavaME could stand to have a bit of introspection, so you could just pass the manager into the constructor.

-Shawn

peter_budo
Offline
Joined: 2006-10-18

Sorry I know I'm tiresome, but I want to know if I implemented correctly the Display handling in accordance with your instructions in other posts or is there anything I misunderstand?

sfitzjava
Offline
Joined: 2003-06-15

I wouldn't say you "misunderstood" anything, there are just many design concepts that can be taken to implement this design. The best recommendation I can provide is for you to think of your parts of code as having certain responsibilities, and then separating that code to those areas to establish your objects. That is NOT to say you take all calculation type methods and put them into some utility class like is done with the Math class in java. But rather things that deal with setup should be in a special setup class.

Also your original post was on how to transfer from one screen to another. Do not use the same code as some magical silver bullet for all design uses. It can fit several solutions, but needs to have at least different implementations.

So you could have a controller with a screen that it used, then your transfer would not be of screens but of controllers that had access to screens.

If you look at the Microbus.dev.java.net source for the sample implementation that might give you some more ideas on how to this technique can be used.

Really once you have a good understanding of writing Java code, it is best to read some books on design, and architecture. Books like Peter Coed's book Java Design: Building Better Apps and Applets. Then maybe James W. Cooper's book Java Design Patterns.

-Shawn