Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Annoying null pointer exception in my code

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Annoying null pointer exception in my code

    I just wrote some codes in two classes just to obtain values fro form controls in lwuit but always return NPE. cCan somebody helpme please. Here is my snippet

    /*
     * Copyright (c)  2012, Zeeders and/or its affiliates. All rights reserved.
     */
     
     
    import com.sun.lwuit.Display;
    import com.sun.lwuit.io.NetworkManager;
    import com.sun.lwuit.io.Storage;
    import javax.microedition.midlet.*;
     
    /**
     * MIDP version of the GP Calculator
     *
     * @author Oyinlola Israel
     */
    public class calc extends MIDlet {
        private LocalApp app = new LocalApp();
     
        public void startApp() {
            Display.init(this);
            NetworkManager.getInstance().start();
            app.startApp();
        }
     
        public void pauseApp() {
        }
     
        public void destroyApp(boolean unconditional) {
        }
    }

    That is the first class called clac.java

    /*
     * Copyright (c) 1990, 2011, Oracle and/or its affiliates. All rights reserved.
     */
     
     
    import com.sun.lwuit.*;
    import com.sun.lwuit.util.EventDispatcher;
    import com.sun.lwuit.events.DataChangedListener;
    import com.sun.lwuit.events.SelectionListener;
    import java.io.IOException;
    import com.sun.lwuit.animations.CommonTransitions;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.io.ConnectionRequest;
    import com.sun.lwuit.io.NetworkManager;
    import com.sun.lwuit.io.services.ImageDownloadService;
    import com.sun.lwuit.io.ui.SliderBridge;
    import com.sun.lwuit.io.util.JSONParser;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.layouts.BoxLayout;
    import com.sun.lwuit.layouts.FlowLayout;
    import com.sun.lwuit.list.DefaultListCellRenderer;
    import com.sun.lwuit.list.ListModel;
    import com.sun.lwuit.plaf.Border;
    import com.sun.lwuit.plaf.Style;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Vector;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
     
     
     
     
    /**
     * Implements the local business search UI of the makeover demo
     *
     * @author  Shai Almog
     */
    public class LocalApp {
        /* Demo mode is activiated if network connection fails */
     
        TextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10, tf11, fname, fsub, tomail, fmail;
        ComboBox cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, cb11;
        TextArea fbody;
        int cvalu;
        float gp;
     
        String rescom, gps;
     
        private boolean started;
        private Command exitCommand = new Command("Exit") {
            public void actionPerformed(ActionEvent ev) {
                Display.getInstance().exitApplication();
            }
        };
     
     /*    Command clacgp = new Command("Calculate GP") {
                public void actionPerformed(ActionEvent ev) {
                    calculategp(Integer.parseInt(tf1.getText()), (String)cb1.getSelectedItem(), 
                            Integer.parseInt(tf2.getText()), (String)cb2.getSelectedItem(),
                            Integer.parseInt(tf3.getText()), (String)cb3.getSelectedItem(),
                            Integer.parseInt(tf4.getText()), (String)cb4.getSelectedItem(),
                            Integer.parseInt(tf5.getText()), (String)cb5.getSelectedItem(),
                            Integer.parseInt(tf6.getText()), (String)cb6.getSelectedItem(),
                            Integer.parseInt(tf7.getText()), (String)cb7.getSelectedItem(),
                            Integer.parseInt(tf8.getText()), (String)cb8.getSelectedItem(),
                            Integer.parseInt(tf9.getText()), (String)cb9.getSelectedItem(),
                            Integer.parseInt(tf10.getText()), (String)cb10.getSelectedItem(),
                            Integer.parseInt(tf11.getText()), (String)cb11.getSelectedItem()
     
                            );
     
                }
            };
            * 
            * 
            */
     
     
         public ComboBox createcombo()
        {
     
        ComboBox n = new ComboBox(new String[] {"A 5", "B 4", "C 3", "D 2", "E 1", "F 0"});
     
        return n;
        }
     
        public TextField createt()
        {
     
            TextField t = new TextField("0", 50);
            return t;
     
        }
     
        private void findgp1()
       {
     
               String m = (String)tf2.getText();
               System.out.println(m);
     
       }
     
     
         Command clacgp = new Command("Calculate GP") {
                public void actionPerformed(ActionEvent ev) {
                    findgp1();
     
                }
            };
     
     
     
        private Command defaultThemeCommand = new Command("Set Theme 1") {
            public void actionPerformed(ActionEvent ev) {
                setTheme("/theme.res");
            }
        };
     
        private Command javaThemeCommand = new Command("Set Theme 2") {
            public void actionPerformed(ActionEvent ev) {
                setTheme("/LWUITtheme.res");
            }
        };
     
        private Command exit = new Command("Exit"){
            public void actionPerformed(ActionEvent ev) {
                   Display.getInstance().exitApplication();
            }
     
        };
     
          private    Command mail = new Command("Send Mail"){
            public void actionPerformed(ActionEvent ev) {
                   mailform();
            } 
          };
     
      private   Command back = new Command("Back"){
            public void actionPerformed(ActionEvent ev) {
                showMainForm();             
        }
      };
     
     
        private void setTheme(String name) {
            try {
                com.sun.lwuit.util.Resources res = com.sun.lwuit.util.Resources.open(name);
                com.sun.lwuit.plaf.UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                Display.getInstance().getCurrent().refreshTheme();
            } catch(java.io.IOException err) {
                 err.printStackTrace();
            }
        }
     
        private static Image getImage(String name) {
            try {
                return Image.createImage(name);
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
        }
     
     
     
     
        public void startApp() {
            // distinguish between start and resume from pause
            if(!started) {
                started = true;
     
                // show your LWUIT form here e.g.: new MyForm().show();
                // this is a good place to set your default theme using
                // the UIManager class e.g.:
                try {
                    com.sun.lwuit.util.Resources res = com.sun.lwuit.util.Resources.open("/theme.res");
                    com.sun.lwuit.plaf.UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                } catch(java.io.IOException err) {
                     err.printStackTrace();
                }
                showMainForm();
            }
        }
     
        private void showMainForm() {
            Form mainForm = new Form("CALCULATE GP");
            mainForm.setTransitionInAnimator(CommonTransitions.createFastSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 400));
            mainForm.setTransitionOutAnimator(CommonTransitions.createFastSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 400));
            mainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            mainForm.addComponent(new Label("IST GRADE AND CREDIT LOAD"));
        final     TextField tf1 = createt();
            mainForm.addComponent(tf1);
        final     ComboBox cb1 = createcombo();
             mainForm.addComponent(cb1);
     
              mainForm.addComponent(new Label("2ND GRADE AND CREDIT LOAD"));
        final     TextField tf2 = createt();
            mainForm.addComponent(tf2);
        final     ComboBox cb2 = createcombo();
             mainForm.addComponent(cb2);
     
            mainForm.addComponent(new Label("3RD GRADE AND CREDIT LOAD"));
        final     TextField tf3 = createt();
            mainForm.addComponent(tf3);
     
        final     ComboBox cb3 = createcombo();
             mainForm.addComponent(cb3);
     
     
            mainForm.addComponent(new Label("4TH GRADE AND CREDIT LOAD"));
        final     TextField tf4 = createt();
            mainForm.addComponent(tf4);
       final      ComboBox cb4 = createcombo();
             mainForm.addComponent(cb4);
     
     
          mainForm.addComponent(new Label("5TH GRADE AND CREDIT LOAD"));
         final    TextField tf5 = createt();
            mainForm.addComponent(tf5);
          final   ComboBox cb5 = createcombo();
             mainForm.addComponent(cb5);    
     
     
        mainForm.addComponent(new Label("6TH GRADE AND CREDIT LOAD"));
         final    TextField tf6 = createt();
            mainForm.addComponent(tf6);
         final    ComboBox cb6 = createcombo();
             mainForm.addComponent(cb6);
     
        mainForm.addComponent(new Label("7TH GRADE AND CREDIT LOAD"));
         final    TextField tf7 = createt();
            mainForm.addComponent(tf7);
         final    ComboBox cb7 = createcombo();
             mainForm.addComponent(cb7);
     
       mainForm.addComponent(new Label("8TH GRADE AND CREDIT LOAD"));
         final    TextField tf8 = createt();
            mainForm.addComponent(tf8);
          final   ComboBox cb8 = createcombo();
             mainForm.addComponent(cb8);       
     
       mainForm.addComponent(new Label("9TH GRADE AND CREDIT LOAD"));
          final  TextField tf9 = createt();
            mainForm.addComponent(tf9);
        final     ComboBox cb9 = createcombo();
             mainForm.addComponent(cb9);       
     
        mainForm.addComponent(new Label("10TH GRADE AND CREDIT LOAD"));
        final     TextField tf10 = createt();
            mainForm.addComponent(tf10);
        final     ComboBox cb10 = createcombo();
             mainForm.addComponent(cb10);      
     
        mainForm.addComponent(new Label("11TH GRADE AND CREDIT LOAD"));
         final    TextField tf11 = createt();
            mainForm.addComponent(tf11);
        final     ComboBox cb11 = createcombo();
             mainForm.addComponent(cb11);      
     
     
     
            mainForm.addCommand(exitCommand);
            mainForm.addCommand(defaultThemeCommand);
            mainForm.addCommand(javaThemeCommand);
     
            mainForm.addCommand(clacgp);
            mainForm.addCommand(mail);
     
     
            mainForm.show();
        }
     
        private void exception(Exception ex) {
            ex.printStackTrace();
            Dialog.show("Error", "Error connecting to search service - Turning on DEMO MODE", "OK", null);
            showMainForm();
        }
     
       private int comboval(String ve)
        {
        char v = ve.trim().charAt(0);
     
            if (v == ('A'))
            {
            int cvalu = 5;  
            }
            else if (v ==('B'))
            {
            int cvalu = 4;  
            }
            else if (v == ('C'))
            {
            int cvalu = 3;  
            }
            else if (v == ('D'))
            {
            int cvalu = 2;  
            }
            else if (v == ('E'))
            {
            int cvalu = 1;  
            }
            if (v == ('F'))
            {
           int cvalu = 0;  
            }
     
        return cvalu;    
     
        }
     
     
     
     /*  private void findgp(int va1, String c1,int va2,String c2,int va3,String c3,int va3,String c3,int va5,String c5,int va6,String c6,int va7,String c7,int va8,String c8,int va9,String c9,int va10,String c10,int va11,String c11 )
       {
              int va1 = Integer.parseInt(tf1.getText());
          String c1 = (String)cb1.getSelectedItem();
          int cv1 = comboval(c1);
     
     
          int va2 = Integer.parseInt(tf2.getText());
          String c2 = (String)cb2.getSelectedItem();
            int cv2 = comboval(c2);
     
          int va4 = Integer.parseInt(tf4.getText());
          String c4 = (String)cb4.getSelectedItem();
            int cv4 = comboval(c4);
     
          int va3 = Integer.parseInt(tf3.getText());
          String c3 = (String)cb3.getSelectedItem();
            int cv3 = comboval(c3);
     
          int va5 = Integer.parseInt(tf5.getText());
          String c5 = (String)cb5.getSelectedItem();
            int cv5 = comboval(c5);
     
          int va6 = Integer.parseInt(tf6.getText());
          String c6 = (String)cb6.getSelectedItem();
            int cv6 = comboval(c6);
     
          int va7 = Integer.parseInt(tf7.getText());
          String c7 = (String)cb7.getSelectedItem();
            int cv7 = comboval(c7);
     
          int va8 = Integer.parseInt(tf8.getText());
          String c8 = (String)cb8.getSelectedItem();
            int cv8 = comboval(c8);
     
          int va9 = Integer.parseInt(tf9.getText());
          String c9 = (String)cb9.getSelectedItem();
            int cv9 = comboval(c9);
     
          int va10 = Integer.parseInt(tf10.getText());
          String c10 = (String)cb10.getSelectedItem();
            int cv10 = comboval(c10);
     
         int va11 = Integer.parseInt(tf11.getText());
          String c11 = (String)cb11.getSelectedItem();
            int cv11 = comboval(c11);
     
                    int denum = va1+va2+va3+va4+va5+va6+va7+va8+va9+va10+va11;
     
                    int num = cv1+cv2+cv3+cv4+cv5+cv6+cv7+cv8+cv9+cv10+cv11;
     
                   float gp = num/denum;
     
                     gps = "Your Grade Point Average is  "+gp;
     
       }
       * 
       * 
       */
     
       private void getcom()
       {
     
     
        //   return rescom;
     
       }
     
     private void  calculategp(int va1, String c1,int va2,String c2,int va3,String c3,int va4,String c4,int va5,String c5,int va6,String c6,int va7,String c7,int va8,String c8,int va9,String c9,int va10,String c10,int va11,String c11 )
        {
     
            int s1 = c1.charAt(3);
            int s2 = c1.charAt(3);
            int s3 = c1.charAt(3);
            int s4 = c1.charAt(3);
            int s5 = c1.charAt(3);
            int s6 = c1.charAt(3);
            int s7 = c1.charAt(3);
            int s8 = c1.charAt(3);
            int s9 = c1.charAt(3);
            int s10 = c1.charAt(3);
            int s11 = c1.charAt(3);
     
      int num = ((va1*s1)+(va2*s2)+(va3*s3)+(va4*s4)+(va5*s5)+(va6*s6)+(va7*s7)+(va8*s8)+(va9*s9)+(va10*s10)+(va11*s11) );
      int den = va1+va2+va3+va4+va5+va6+va7+va8+va9+va10+va11; 
     
      float gp = num/den;
      String gps = "Your Calculated Grade Point Is  "+gp;
     
       if(gp>=4.5){ String rescom = "Your parent must hear about this. Having first class is not an easy thing. Congratulation";
     
       }
       else if (gp >=3.5){ String rescom = "Well, second class upper is not a bad deal but you can so much be better";
     
       } 
       else if (gp >=3.0){ String rescom = "What's up, why is is like this. Sure everything is alright?";
     
       } 
       else if (gp >=1.5){ String rescom = "Is this what you really saw on the board? If it is, it isn't a mistake, just keep silent and pretend.";
     
       } 
    else if (gp < 1.5){ String rescom = "Believe me, you can find a better thing to do with your life that to waste precious moments at school. Pack your luggages from school and find something else to do. You might also be a billionaire";
     
    } 
     
              Form res = createForm("GP RESULT"); 
               res.setTransitionInAnimator(CommonTransitions.createFastSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 400));
            res.setTransitionOutAnimator(CommonTransitions.createFastSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 400));
             res.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
     
     
     
     
            Label re = new Label(gps);
            re.getStyle().setPadding(5, 5, 5, 5);
            re.getStyle().setBgColor(930);
     
     
            Image le = getImage("learn.png");
            Image logo = getImage("logo.png");
     
            Label com = new Label (rescom);
            com.getStyle().setPadding(10, 0, 0, 0);
            com.getStyle().setBgImage(le);
     
             res.addComponent(re);
             res.addComponent(com);
     
     
             res.addCommand(back);
             res.addCommand(mail);
     
             res.show();
        }   
     
     public void mailform()
     {
     Form mform = new Form("ZEEDERS MAIL");
     Label inst = new Label("SEND EASY MAIL");
     
     Label namel = new Label("Your Name");
     final TextField fname = new TextField(50);
     
     Label maill = new Label("Your Mail Address");
     final TextField fmail = new TextField(50);
     
     Label tomaill = new Label("To E-mail");
      final TextField tomail = new TextField(50);
     
     Label subl = new Label("Your Subject");
     final TextField fsub = new TextField(50);
     
     Label bodyl = new Label("Your Message Body");
     final TextArea fbody = new TextArea(5, 20, TextArea.ANY);
     
     mform.addComponent(inst);
     mform.addComponent(namel);
     mform.addComponent(fname);
     mform.addComponent(maill);
     mform.addComponent(fmail);
     mform.addComponent(tomaill);
     mform.addComponent(tomail);
     mform.addComponent(subl);
     mform.addComponent(fsub);
     mform.addComponent(bodyl);
     mform.addComponent(fbody);
     
     Command send = new Command("Send Mail")
     {
        public void actionPerformed(ActionEvent ev)
        {
        sendmail();
        }
     };
     
     mform.addCommand(send);
     
     Command nback = new Command("Back")
     {
        public void actionPerformed(ActionEvent ev)
        {
        showMainForm();
        }
     };
     
     mform.addCommand(nback);
     
     mform.show();
     
     } 
     
     public void sendmail()
     {
                         try{
     
            String name = fname.getText();
            String mail = fmail.getText();
            String to = tomail.getText();
            String sub= fsub.getText();
            String body = fbody.getText();
            String url="http://www.zeeders.com/app/app_mail_n.php?name="+name+"&mail="+mail+"&to="+to+"&subject="+sub+"&body="+body;
            url = url.replace(' ', '_');
     
     
    	HttpConnection conn = (HttpConnection)Connector.open(url);
     
    InputStream  ins = conn.openInputStream();
     
    	InputStreamReader xmldata = new InputStreamReader(ins);
     
            if(xmldata.equals("true")){
               Dialog.show("Mail Sent", "Your Mail has been successfully sent", "OK", null);
            }
            else{
                   Dialog.show("Error", "Your mail could not be delivered  to the receipient", "OK", null);
            }
     
             ins.close();
             conn.close();
           }
           catch(IOException ex){
     
               System.out.print("IOException occured");
     
           }
     }
     
     
        private Label createSubLabel(String text, int fgColor, Font f) {
            Label l = new Label(text);
            Style s = l.getStyle();
            s.setFgColor(fgColor);
            s.setFont(f);
            return l;
        }
     
     
     
     
        private Form createForm(String title) {
            Form f = new Form(title);
            f.getTitleComponent().setAlignment(Component.LEFT);
            f.setMenuCellRenderer(new DefaultListCellRenderer(false));
            return f;
        }
     
        /**
         * A list model that lazily fetches a result over the web if its unavailable
         */
     
     
     
     
     
     
     
    }

    And this is the second class called LocalApp.java

    Although I have changed a lot of things but the central idea is just that I cant get values from the gui control.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Annoying null pointer exception in my code

    The key bit of information is: which line causes the NPE to be thrown?

Similar Threads

  1. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 10:21 AM
  2. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  3. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  4. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM