Forums
Welcome to Live View – Take the tour to learn more
Start Tour
There is currently 1 person viewing this thread.
Ovalman.
30 Jan 17 21:12
Joined:
Date Joined: 29 Nov 02
| Topic/replies: 14,508 | Blogger: Ovalman.'s blog
Anyone help me?

Stackoverflow seems so condescending, not a place for a noob. I've asked on another group but it doesn't seem a busy group. Clutching straws with Chit Chat but surely someone has a bit of programming knowledge.

I'm trying to pass a date into a String via a method but I keep returning a null value.

        getDateTime();  // this keeps returning null
        balanceToDatabase(balance, strId);
        helper.insertIntoPaid(strId, amountpaid, dateTime, "Notes Paid", "Spare 1", "Spare 2", "Spare 3"); //insert into paid table


        btnPaid.setClickable(false); // so paid button can't be pressed again to avoid mistakes

        Toast.makeText((getApplicationContext()), "£" + amountpaid + " paid has been recorded!", Toast.LENGTH_SHORT).show();
    }

        public void balanceToDatabase (String amountResultStr, String strId){
        ContentValues newValues = new ContentValues();
        newValues.put("balance", amountResultStr);
        SQLiteDatabase db= helper.getWritableDatabase();
        db.update("customers", newValues, "_id="+ strId, null);
        db.close();
    }

        public String getDateTime() {
            Calendar c = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            String dateTime = df.format(c.getTime());
            // formattedDate have current date/time
            Toast.makeText(this, dateTime, Toast.LENGTH_SHORT).show();  // This is showing the date time when button clicked

            return dateTime;
        }
Pause Switch to Standard View Android programming help
Show More
Loading...
Report mememe January 30, 2017 9:15 PM GMT
It's a problem with the clacker.

Switch off, wait 10 seconds and then go to the pub.

HTH
Report Ovalman. January 30, 2017 9:21 PM GMT
I was waiting on a funny answer Tongue Out

I know Chit Chat is not a good place to ask such things but I'm hoping just one Chit Chatter can help.

Hoping someone can point me in the direction of a noobs forum where I can ask such questions.
Report saddo January 30, 2017 9:29 PM GMT
I was going well until I got past "trying" Sad
Report Charlie January 30, 2017 9:33 PM GMT
Is it because dateTime is local to getDateTime().
Report Ovalman. January 30, 2017 9:39 PM GMT
It's only took me 3 long years to get to making my own programs and I've nothing (yet) on the Play Store. Never give up Saddo, you will get there one day!

When you ask such questions on Stack Overflow, you always get some condescending expert that find a fault in your question. If I knew the bloody answer I wouldn't be asking the question. I'm scared to ask over there TBH as my first and only question was downvoted for being a noob question.

I'd love to find a simple forum where you can ask such questions. Despite searching I've yet to find a decent site. Google Groups is about the best I could find.
Report Ovalman. January 30, 2017 9:42 PM GMT
Thanks Charlie, I thought because I made the method public it returned dateTime as a String? I've tried passing in a String as part of the method but I'm unsure how to return an actual value. Any help as to completing the code?
Report Charlie January 30, 2017 10:04 PM GMT
Ovalman it's been a few years since I did any programming and never on Android. Even though getDateTime is public I don't think it's variables are public (I may be wrong though!).

What happens if you put Public String dateTime above your first line ( getDateTime() ) and remove String from String dateTime = df.format(c.getTime());
Report Ovalman. January 30, 2017 10:24 PM GMT
I'm not sure I entirely understand your post Charlie but I've joined Androidforums who have a small but active programming section. I've asked over there.

I know it's not something to complicated for my answer. OOP does my head in. I grew up in the 80's where Spaghetti coding was all the rage. I'm much more used to BASIC than I am to Java but I need to learn Java as my programming ideas suit a mobile platform.

As I said, I've only been trying for 3 years! I am getting my head around things now and can make apps that actually work.
Report detraveller January 30, 2017 10:28 PM GMT
I do all my android programming on iOS you should move there too. Its much easier.
Report detraveller January 30, 2017 10:47 PM GMT
Ok here is what I am thinking. I can do some java but not android, and dont know if there is a big difference. I just checked how java stores date and time. In that class, there is a separate method that converts the date to a string! So its not a given that the date is stored as a string.

In your program, df.format(c.getTime()); is returning a string, are you sure about that?

When you declare a variable, it contains null(I think :)). So if your method is returning null, it means the line df.format(c.getTime()); is not putting any value in the variable String dateTime. Why don't you try initialising String dateTime with a value, ex. "hello". Then if your method, is returning "hello", you at least know df.format(c.getTime()); ain't doing shlt.

I really suspect the line df.format(c.getTime()); to be the main issue here. At least show us what the method format() is doing.
Report Ovalman. January 30, 2017 11:07 PM GMT
I'm making a toast of the string which is a pop up to tell me what the variable is. Toast.makeText(this, dateTime, Toast.LENGTH_SHORT).show();

dateTime is my String. I'm returning the String in the next line: return dateTime;

I think I've set up the method wrong. I tried it this way and still get null:

        public String getDateTime(String dt) {
            Calendar c = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            String dateTime = df.format(c.getTime());
            // dateTime has current date/time
            Toast.makeText(this, dateTime, Toast.LENGTH_SHORT).show(); // The variable dateTime is a String but the method returns null

            dt=dateTime;
            return dt;
        }
Report detraveller January 30, 2017 11:27 PM GMT
You didn't answer this: In your program, df.format(c.getTime()); is returning a string, are you sure about that?

You can easily put values into the vriables you are returning to see exactly which line is giving the error. Did you understand that part? Or should i explain it again?
Report Ovalman. January 30, 2017 11:50 PM GMT
Yes, I'm running it in debugging mode and dateTime="30-01-2017 23:42:52"

I changed dateTime to "test" and still getting a null value. Passing the value from my method or calling the method seems to be the problem.
Report detraveller January 30, 2017 11:57 PM GMT
Could this be the problem?

getDateTime(); returns a value, namely dateTime. But when you call the method in your program, it does return the value. But where should it return the value to? it does return, but there is no variable to put the value into.

Try String oval = getDateTime(); in the first line

Now oval should have the date in it.
Report detraveller January 30, 2017 11:58 PM GMT
Sorry for my horrible english in the second line. Butts galore.
Report detraveller January 31, 2017 12:07 AM GMT
Basically when you 'get' some value, you have to put it into a variable to use it later.

When you get a pack of milk from the market, you need a bag(or hands) to put it in and bring home.

In the first line of your code, you are saying you are getting the packet of milk, but you are leaving it at the counter and wondering where the milk is when you reach home. You need to put the milk in the bag(variable) and bring it home. Only then can you use the milk.
Report Ovalman. January 31, 2017 12:09 AM GMT
I might have solved it and you were correct.

String dateTime = df.format(c.getTime()).toString();

Toast was allowing dateTime as a local String hence it showing as a Toast it wasn't being passed as a String back to my method call. I added .toString(); and I think it has cured the problem.

When I ran it in debugging the variable was inserted into my database according to debugging. Need to write some code to get the data back out of the database to make sure.

Sqlite is a bugger for Android unless your device is rooted so you can examine the database directly to check for values.
Report detraveller January 31, 2017 12:12 AM GMT
Yes I was talking about the toString() method.

But is your first line of code now working? You need to put the result of that line in some variabe. Otherwise it doesn't make sense. It gets the value and then has nowhere to put it in.
Report Ovalman. January 31, 2017 12:17 AM GMT
helper.insertIntoPaid(strId, amountpaid, dateTime, "Notes Paid", "Spare 1", "Spare 2", "Spare 3"); //insert into paid table

But it's a public variable and things should be private whenever possible but the whole code I'm using is inside a button so nothing happens until the button is pressed.

I sorted the notes column while I was checking elsewhere for this solution. That updates my paid table in my database. The spares are just that in case I need them for anything in future, that's why I reference them directly as strings.

Another wee bit done, as I said it's only been 3 bloody years!
Report detraveller January 31, 2017 12:19 AM GMT
I get started next month on android prog.
Report Ovalman. January 31, 2017 12:33 AM GMT
I've done Python and C# courses, both of which are easier than android but it's Android I want to develope for.

I tried many Youtube and online courses but it wasn't until I done a couple of free courses on Udacity that things started to make sense. I still struggle with implementing Object Orientated Programming, it's totally alien to what I grew up with.

I'm guessing your doing a computer science degree? My son is 2nd year of Computer games programming at Coleraine University, I guess it was him that inspired me to try and learn it again. I've a few apps including this one that I think I could monetise.

You've been a great help Detraveller and thanks also Charlie for the response. I'm still waiting on someone to answer this problem on that Android forum I joined so Chit Chat comes up trumps again.
Report zorrostrikes January 31, 2017 1:47 AM GMT
what you need is more negative integers. get a pepper pot and sprinkle them in.
Report detraveller January 31, 2017 2:35 AM GMT
I am studying maths at University. Actuarial Science to be exact.
Report sixtwosix January 31, 2017 7:57 AM GMT
Crikey , never thought I would see code on here and a programming question , as an IT veteran of 35 years and a grizzled old programmer of the 1980's , it is a joy to see.Cool
Report Smar Tarse January 31, 2017 12:41 PM GMT
Ovalman - if you are from a BASIC programming background and now you want to program for Android then maybe Bassic4Android is the "easiest" option for you https://www.b4x.com/
Report Smar Tarse January 31, 2017 12:43 PM GMT
*Basic4Android
Report Ovalman. January 31, 2017 5:33 PM GMT
Thanks for the replies guys.

Smar Tarse, I've been through all those programs, Google's Appinventor will have you creating apps in minutes but it's not what I need. I've took many courses but only when I done this course did it all make sense. It brings everything straight down to basics:

https://www.udacity.com/course/android-development-for-beginners--ud837

There's a few courses more advanced but getting that under my belt made things so much easier. I can thouroughly recommend it. If I'd have took that at the start instead of following coders on Youtube then I wouldn't have struggled so much.

I'd love to find a decent forum where I could ask questions like above without getting flamed because the question has been asked before. Don't they think I already tried searching for the problem?
Report Hamsterdam January 31, 2017 6:35 PM GMT
I'm astonished you got an answer to this on here tbh! You can always try google the question and type forum after it you just have to know how to manipulate the search terms.
Report Ovalman. January 31, 2017 7:25 PM GMT
That's the problem. Most of the time it is about Googling and adapting other peoples code but sometimes you just get stuck and can't see the problem if it stares you straight in the face. I was getting nowhere on Facebook and I didn't want to ask on Stack Overflow so I though Chit Chat may help or at least point me to help. Detraveller pointed me in the right direction even if he couldn't see it directly himself.

Chit Chat wins the day.
Post Your Reply
<CTRL+Enter> to submit
Please login to post a reply.

Wonder

Instance ID: 13539
www.betfair.com