0

Possible Duplicate:
NoClassDefFoundError importing a library project

My blackberry application suddenly stopped working. My simulator or/and my application does not load up, I get the " Uncaught Exception: java.lang.NoClassDefFoundError ". I have not made any changes to my code since I last tested it. What has gone wrong?

package mypackage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Vector;

import javacard.framework.UserException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.component.pane.TitleView;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.image.Image;

public class LoginTest extends UiApplication 
{

    public static void main(String[] args)  
    {
         //Create a new instance of the app
         //and start the app on the event thread.

        LoginTest app = new LoginTest();
        app.enterEventDispatcher();
    }

    public LoginTest()
    {
         //Display a new screen.
         pushScreen(new LoginTestScreen());
    }

}

//Create a new screen that extends MainScreen and provides
//behaviour similar to that of other apps.

final class LoginTestScreen extends MainScreen
{
   //declare variables for later use
   private InfoScreen _infoScreen;
   private ObjectChoiceField choiceField;
   private int select;
   BasicEditField username;
   PasswordEditField passwd;
   CheckboxField checkBox1;
   ButtonField loginBtn;


   public LoginTestScreen()
   {
        //Invoke the MainScreen constructor.

        super();

        //Add a screen title.

        setTitle("Track24ELMS");

        LabelField login = new LabelField("ELMS Login", LabelField.FIELD_HCENTER); 
        login.setFont(Font.getDefault().derive(Font.BOLD, 30));
        login.setMargin(10, 0, 20, 0); //To leave some space from top and bottom

        HorizontalFieldManager user = new HorizontalFieldManager();
        user.setMargin(0, 0, 10, 0);
        HorizontalFieldManager pass = new HorizontalFieldManager();
        pass.setMargin(0, 0, 20, 0);
        HorizontalFieldManager checkbox = new HorizontalFieldManager();
        checkbox.setMargin(0, 0, 30, 0);
        HorizontalFieldManager btns = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);

        LabelField usernameTxt = new LabelField("Username :");
        LabelField passwordTxt = new LabelField("Password :");

        username = new BasicEditField();
        passwd = new PasswordEditField();

        loginBtn = new ButtonField("Login", ButtonField.CONSUME_CLICK); 
        loginBtn.setChangeListener(new LoginButtonListener());

        checkBox1 = new CheckboxField("Remember me",false);

        user.add(usernameTxt);
        user.add(username);
        pass.add(passwordTxt);
        pass.add(passwd);
        btns.add(loginBtn);
        add(login);
        add(user);
        add(pass);
        add(checkBox1);
        add(btns);

}

   private class LoginButtonListener implements FieldChangeListener {
       public void fieldChanged(Field field, int context) {
//Open a new screen

       String uname = username.getText();
       String pwd = passwd.getText();
       String sep = "</>";
       //If there is no input
       if (uname.length() == 0 || pwd.length()==0)
       Dialog.alert("One of the textfield is empty!");
       else 
       {
           String URL = "http://xxx.xxx.com/xxx/xxx.asmx";
           String METHOD_NAME = "ValidateCredentials";
           String NAMESPACE = "http://tempuri.org/";
           String SOAP_ACTION = NAMESPACE+METHOD_NAME;
           //final String URL = "http://prerel.track24elms.com/Android/T24AndroidLogin.asmx/ValidateCredentials";

           SoapObject resultRequestSOAP = null;
           HttpConnection httpConn = null;
           HttpTransport httpt;
           System.out.println("The username" + uname + "password" + pwd );
           SoapPrimitive response = null;
           SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           //String usernamecode = Base64.encode(uname.getBytes());
           //String pwdEncodeString = Base64.encode(pwd.getBytes());
           request.addProperty("username", uname);
           request.addProperty("password", pwd);
           System.out.println("The request is=======" + request.toString());
           SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
           //envelope.bodyOut = request;
           envelope.dotNet = true;
           //envelope.encodingStyle = SoapSerializationEnvelope.XSD;
           envelope.setOutputSoapObject(request);
           System.out.println("The envelope has the value++++"+ envelope.toString());

          /* URL+  Here you can add paramter so that you can run on device,simulator etc. this will work only for wifi */
           httpt = new HttpTransport(URL+ ";deviceside=true;ConnectionUID=S TCP");
           //httpt.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
           httpt.debug = true;
           try
           {
               System.out.println("SOAP_ACTION    ==   " + SOAP_ACTION);
               httpt.call(SOAP_ACTION, envelope);
               response = (SoapPrimitive) envelope.getResponse();
               String result =  response.toString();
               System.out.println("response    ==   " + result);
               resultRequestSOAP = (SoapObject) envelope.bodyIn;
               System.out.println("result    ==   " + resultRequestSOAP); 

               String[] listResult = split(result, sep);


           }
           catch (IOException e) {
               // TODO Auto-generated catch block
               System.out.println("The exception is IO==" + e.getMessage());
           } catch (XmlPullParserException e) {

               // TODO Auto-generated catch block
               System.out.println("The exception xml parser example==="
                       + e.getMessage());
           }
           System.out.println( resultRequestSOAP);
           //UiApplication.getUiApplication().pushScreen(new InfoScreen()); //Open a new Screen

       }
       }

       };

     private String[] split(String original, String separator) {
            Vector nodes = new Vector();
            int index = original.indexOf(separator);
            while (index >= 0) {
                nodes.addElement(original.substring(0, index));
                original = original.substring(index + separator.length());
                index = original.indexOf(separator);
            }
            nodes.addElement(original);
            String[] result = new String[nodes.size()];
            if (nodes.size() > 0) {
                for (int loop = 0; loop < nodes.size(); loop++) {
                    result[loop] = (String) nodes.elementAt(loop);
                    System.out.println(result[loop]);
                }
            }
            return result;
       }

   //To display a dialog box when a BlackBerry device user
   //closes the app, override the onClose() method.

   public boolean onClose()
   {
       Dialog.alert("Goodbye!");
        System.exit(0);
        return true;
   }

   //Create a menu item for BlackBerry device users to click to see more 
   //information about the city they select.

   private MenuItem _viewItem = new MenuItem("More Info", 110, 10) 
   {
        public void run() 
        {
             //Store the index of the city the BlackBerry device user selects

             select = choiceField.getSelectedIndex();

             //Display a new screen with information about the
             //city the BlackBerry device user selects

                 _infoScreen = new InfoScreen();
                UiApplication.getUiApplication().pushScreen(_infoScreen);
        }
   };

   //Create a menu item for BlackBerry device users to click to close 
   //the app.

   private MenuItem _closeItem = new MenuItem("Close", 200000, 10) 
   {
        public void run()
        {
             onClose();
        }
   };


   //Create an inner class for a new screen that displays
   //information about the city a BlackBerry device user selects.

   private class InfoScreen extends MainScreen
   {
       String latitude, logitude, altitude;

        public InfoScreen() 
        {
             super();

             setTitle("Itinerary");


             LabelField login = new LabelField("Employee Itinerary", LabelField.FIELD_HCENTER); 

             HorizontalFieldManager userDetails = new HorizontalFieldManager();
             userDetails.setMargin(0, 0, 10, 0);
             HorizontalFieldManager statusBox = new HorizontalFieldManager();
             statusBox.setMargin(0, 0, 20, 0);
             HorizontalFieldManager locationDetails = new HorizontalFieldManager();
             locationDetails.setMargin(0, 0, 30, 0);
             HorizontalFieldManager imgBtn = new HorizontalFieldManager();
             imgBtn.setMargin(0, 0, 40, 0);

             //BitmapField userImg = new BitmapField(Bitmap.getBitmapResource("img1.jpg"));
             LabelField userFetched = new LabelField("Sarah Farukh");
             LabelField lastStatus = new LabelField("I am OK");
             EditField statusMsg = new EditField("Status Message", "Update status here");

             EditField lat = new EditField("Latitude", "latitude");
             EditField longi = new EditField("Longitude", "logitude");
             EditField alti = new EditField("Attitude", "altitude");

             //BitmapField btnOK = new BitmapField(Bitmap.getBitmapResource("ok.png"),Field.FIELD_BOTTOM);
             //BitmapField btnNO = new BitmapField(Bitmap.getBitmapResource("no.png"),Field.FIELD_BOTTOM);
             //BitmapField btnHN = new BitmapField(Bitmap.getBitmapResource("hn.png"),Field.FIELD_BOTTOM);

             //userDetails.add(userImg);
             userDetails.add(userFetched);
             userDetails.add(lastStatus);
             statusBox.add(userFetched);
             locationDetails.add(lat);
             locationDetails.add(longi);
             locationDetails.add(alti);
             //imgBtn.add(btnOK);
             //imgBtn.add(btnNO);
             //imgBtn.add(btnHN);
             add(login);
             add(userDetails);
             add(statusBox);
             add(locationDetails);
             add(imgBtn);


        }
   }
}
Community
  • 1
  • 1
Sarah
  • 1,895
  • 2
  • 21
  • 39
  • I'd always clean/rebuild/restart simulator when something like this happens. But, assuming the problem doesn't go away .. can you tell us more about that message? **Where** does it throw this exception? You might need to put a `try{} catch (Throwable t) { t.printStackTrace(); }` block in your `main()` method. – Nate Jul 08 '12 at 11:26
  • During debug, the error is first thrown at " loginBtn.setChangeListener(new LoginButtonListener()); ". I have checked the build path and the library is correctly included. – Sarah Jul 08 '12 at 11:48
  • Hmmm. So, it probably can't find `LoginButtonListener`, which is one of your own classes. I'd probably **clean** the simulator, and restart it. – Nate Jul 08 '12 at 11:50
  • Hi Nate, tried everything, still unable to resolve. I have put the try{}catch in order to see what the reason might be but it skips it and throws NoClassDefFoundError on the main class LoginTest app = new LoginTest(); and also at pushScreen(new LoginTestScreen()); – Sarah Jul 08 '12 at 12:37
  • I put try{}catch blocks on each of the lines where the "NoClassDefFoundError" occured and now the application is loading and I am able to debug now. What does this mean? – Sarah Jul 08 '12 at 13:22
  • what are you doing **inside** the `catch()`? – Nate Jul 08 '12 at 13:25
  • try { LoginTest app = new LoginTest(); app.enterEventDispatcher(); }catch(Exception e) { System.out.println("NoClassDefFoundError: "+e); } – Sarah Jul 09 '12 at 05:03
  • You mean the application is working normally now? Or does it throw an exception on the `LoginTest app = new LoginTest();` line, then go to the exception handler, print out "NoClassDefFoundError:" (+e) and never start the main UI processing loop? – Nate Jul 09 '12 at 07:07
  • No Nate, I mean the application is working normally now, just by adding the try{}catch blocks, it is working fine now. – Sarah Jul 09 '12 at 07:44
  • 2
    ... dunno what you guys think, but my feeling is that somewhere along this path, the environment was cleaned or rebuilt, or simulator loading fixed... Welcome to BB Dev! Always remember the BB Dev Mantra: "clean, rebuild, restart simulator". It will get you out of all manner of trouble. Oh, and shutting down Eclipse helps sometimes too. – Richard Le Mesurier Jul 09 '12 at 08:11
  • If Richard is right, why not try to remove the try catch blocks again, and see if it breaks. If it keeps working, then I think it was a problem with something corrupted in the simulator's data files, and cleaning or rebuilding or restarting fixed it. – Nate Jul 09 '12 at 08:16

1 Answers1

2

It is possible that you are accessing a class in a separate library. You have set up your project so that the class is found at build time.

If that library was not deployed to the device for some reason (forgot to? error when deploying?), then your class will not be found at runtime.

Either make sure to deploy this library properly onto the device (by uploading the correct compiled COD file). Or include the library into your project by setting up your build path correctly.

(my answer based on this one: NoClassDefFoundError importing a library project)

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255