0

i am initializing loader in insideOnclick method of button but failed to do so and getting error becuase of third last parameter when passing "null" it gives me error while passing "this" also failed to do work.

    loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View arg0) {
                    if(userName.getText().length() == 0 || password.getText().length() == 0)
                {
            Toast.makeText(context,"UserName Or Password Should be Filled",Toast.LENGTH_SHORT).show();
            }
        else{
    userName.setText("");
   password.setText("");
   **getLoaderManager().initLoader(1,null,null);** //here is the error 

                    }
                }
                });

            }
             @Override
                public  Loader<User> onCreateLoader(int id,Bundle args) {
                 loginLoader = new LoginLoader(context,userName.getText().toString(),password.getText().toString(),"2013-07-10 01:18:26");
                Log.i("login loader",""+loginLoader);
                 return loginLoader;
                 }

                @Override
                public void onLoadFinished(Loader<User> arg0,User proposals) {
                    Log.i("User",""+proposals.getUsername());
                    }

Here is the error List:

01-27 23:27:33.480: E/AndroidRuntime(7777): FATAL EXCEPTION: main 01-27 23:27:33.480: E/AndroidRuntime(7777): java.lang.NullPointerException 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.createLoader(LoaderManager.java:544) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:553) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.initLoader(LoaderManager.java:607) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.mrfs.android.surveyapp.activities.LoginActivityService$1.onClick(LoginActivityService.java:58) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.view.View.performClick(View.java:4211) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.view.View$PerformClick.run(View.java:17267) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Handler.handleCallback(Handler.java:615) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Handler.dispatchMessage(Handler.java:92) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Looper.loop(Looper.java:137) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.ActivityThread.main(ActivityThread.java:4898) 01-27 23:27:33.480: E/AndroidRuntime(7777): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 23:27:33.480: E/AndroidRuntime(7777): at java.lang.reflect.Method.invoke(Method.java:511) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 01-27 23:27:33.480: E/AndroidRuntime(7777): at dalvik.system.NativeStart.main(Native Method) *EDIT CODE: LOGIN LIST SERVICE.JAVA*

public class LoginListService 
{
SurveyDBHelper surveyDBHelper;
private final  static  LoginListService INSTANCE = new LoginListService();
public LoginListService()
{}
public static LoginListService getLoginInstance()
{return INSTANCE;
}
public User getLoginResult(String userName, String password,
            String apkVersion) {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
        formData.add("username", "kong");
        formData.add("password", "kongkong");
        formData.add("apkStatusDate", "2013-07-10 01:18:26"); // **HERE IS THE ERROR** 
        System.out.print("form data values:" + formData);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
                formData, WorkflowRestService.getInstance().getRequestHeaders());
        ResponseEntity<UserListItemHolder> responseEntity = WorkflowRestService
                .getInstance()
                .getRestTemplate()
                .exchange(WorkflowApp.getServicesURL() + "user/logIn",
                        HttpMethod.POST, requestEntity,
                        UserListItemHolder.class);

        Log.i("response Entity Login", "" + responseEntity);
        UserListItemHolder userListItemInstance = responseEntity.getBody();
        Log.i("response Entity Body Location Function",
                "" + responseEntity.getBody());
        if ("true".equals(userListItemInstance.getApkStatus())) {
Log.i("locationInstance.getLocationListItems if", ""+ userListItemInstance.getUserListItems());
            return userListItemInstance.getUserListItems();


        } else {
Log.i("locationInstance.getLocationListItems else", ""+ userListItemInstance.getUserListItems());
            userListItemInstance = null;
            return userListItemInstance.getUserListItems();
        }

    }
user3233280
  • 279
  • 2
  • 7
  • 21

2 Answers2

1

Looking at your code and your marker, the NullPointerException occurs because you're passing in a null in the place of a required parameter.

getLoaderManager().initLoader(1,null,null); //here is the error

According to the LoaderManager Docs the callback is required:

Parameters

  • id A unique identifier for this loader. Can be whatever you want. Identifiers re scoped to a particular LoaderManager instance.
  • args Optional arguments to supply to the loader at construction. If a loader already exists (a new one does not need to be created), this parameter will be ignored and the last arguments continue to be used.
  • callback Interface the LoaderManager will call to report about changes in the state of the loader. Required.

It looks like you're already implementing LoaderCallbacks, you just have to pass your implementation to the initLoader method:

getLoaderManager().initLoader(1,null,this);
copolii
  • 14,208
  • 10
  • 51
  • 80
  • now this error is removed but still null pointer exception in my service class that is reponsible to bring data from Service – user3233280 Jan 27 '14 at 08:36
  • Parsing time as a string can be a huge pain you'll have to deal with locales, formatting, etc. I would recommend to instead pass the time millis values (System.currentTimeMillis()). It's less human readable, but can easily be formatted a string where it needs to be displayed to users. – copolii Jan 27 '14 at 08:40
  • now i have change datetime datatype to String free from erros but still have little problems can someone help me – user3233280 Jan 27 '14 at 08:46
0

Your date is in the wrong format. Use this instead:

2013-02-03T06:41:41.000Z

You have to put a T between date and time, a Z to the end, and add milliseconds.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2013-02-03 06:41:41': not a valid representation (error: Failed to parse Date value '2013-02-03 06:41:41': Can not parse date "2013-02-03 06:41:41": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd") (at offset 10)) 01-27 23:44:44.195: E/AndroidRuntime(13215): at [Source: libcore.net.http.ChunkedInputStream@421fdb78; line: 1, colum – user3233280 Jan 27 '14 at 07:59
  • the isssue is when i am trying to pass date to webservice let me edit code – user3233280 Jan 27 '14 at 08:07
  • THE ERRROR IS in added file LoginListService.java – user3233280 Jan 27 '14 at 08:10
  • I dnt want to change date, date should be same becuase i am getting data from service on the basis of this date – user3233280 Jan 27 '14 at 08:27
  • This represents the same date, it's just in a different format. – FD_ Jan 27 '14 at 08:39