简体   繁体   中英

Date datatype in parse

I am trying to send date datatype in to Parse.com database. I am neither able to send data or get any app crash message, not sure what is going on..

This is the code.

   buttonPlacebet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ParseUser currentUser = ParseUser.getCurrentUser();
          /**  if (currentUser != null)
            {

            }
            else
            {
                // show the signup or login screen
            } **/

            final String Betname = etBetName.getText().toString().trim();
            final  String BetDescription = etDescription.getText().toString().trim();
            final int Betvalue = seekBarValue.getProgress();
            final String Cat1 = cat1.getSelectedItem().toString();
            final String Cat2 = cat2.getSelectedItem().toString();

            String endDate = textEndDate.getText().toString();

            // = "03/26/2012 11:49:00 AM";
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date convertedDate = new Date();
            try {
                convertedDate = dateFormat.parse(endDate);
                actualendDate = convertedDate;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.d(TAG, "onCreate() Restoring previous state");
            ParseObject bets = new ParseObject("Bets");
            bets.put("BetName", Betname);
            bets.put("BetDescription", BetDescription);

            bets.put("BetValue",Betvalue);
            if(cbPvtbet.isChecked())
            {
                bets.put("PrivateBet","True");
            }
            else
            {
                bets.put("PrivateBet","False");
            }
            if(cbReal.isChecked())
            {
                bets.put("RealBet","True");
            }
            else
            {
                bets.put("RealBet","False");
            }

            bets.put("Cat1",Cat1);
            bets.put("Cat2",Cat2);

            endDate = actualendDate.toString();

            bets.put("EndDate",actualendDate);

            bets.saveInBackground();
        }
    });

}

I am guessing I am doing something wrong with the date datatype not converting/parsing properly. Tks

Adding Log cat (right when I press the button), but frankly there is literally nothing in the logcat that is showing up..

05-30 21:49:25.652: E/AndroidRuntime(27738): FATAL EXCEPTION: main
05-30 21:49:25.652: E/AndroidRuntime(27738): Process: com.techiequickie.bharath.betonanything, PID: 27738
05-30 21:49:25.652: E/AndroidRuntime(27738): java.lang.NullPointerException
05-30 21:49:25.652: E/AndroidRuntime(27738):    at com.techiequickie.bharath.betonanything.NewBet$1.onClick(NewBet.java:136)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.view.View.performClick(View.java:4480)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.view.View$PerformClick.run(View.java:18686)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.os.Handler.handleCallback(Handler.java:733)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.os.Looper.loop(Looper.java:157)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at java.lang.reflect.Method.invokeNative(Native Method)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at java.lang.reflect.Method.invoke(Method.java:515)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
05-30 21:49:25.652: E/AndroidRuntime(27738):    at dalvik.system.NativeStart.main(Native Method)

As your log shows NullPointerException try to look at all your variables, especially on the actualendDate cause you're trying to initialize it with date parsed from string input. And this is the place where everything's gone wrong.

Look at your sample date input: "03/26/2012 11:49:00 AM" and compare it with pattern you provided for SimpleDateFormat: "yyyy-MM-dd" . They're completely different. That's why ParseException's risen and your actualendDate is not initialized and refers to Null

So the solution is to look at SimpleDateFormat documentation and change

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

to

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM