Referencing Problem

I'm having a bit of a problem with my code. I cannot a solution to my problem.

I'm trying to reference to the Reformat() method, but I get an error saying:

"The local variable date may not have been initialized"

import java.util.Calendar;

import java.util.GregorianCalendar;

publicclass DateUtil

{

Calendar calendar;

GregorianCalendar greg;

public DateUtil()

{

}

publicvoid GregorianCalendar(int year,int month,int date)

{

System.out.println("DATE: " + calendar.get(Calendar.DATE) +"-" +

calendar.get(Calendar.MONTH) +"-" + calendar.get(Calendar.YEAR));

}

public String Reformat()

{

String jan ="Jan";

String feb ="Feb";

if(calendar.MONTH > 0)

{

if(calendar.MONTH == 1)

return jan;

if(calendar.MONTH == 2)

return feb;

}

returnnull;

}

public String Storage()

{

DateUtil date;

String nString;

nString = date.Reformat();

returnnull;

}

}

[2211 byte] By [vopoa] at [2007-11-27 7:39:11]
# 1
you call methods on calendar whereas you have not initialized (this.calendar = Calendar.getInstance()) it
calvino_inda at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 2
what does (= Calendar.getInstance()) do?
vopoa at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 3
it initializes the objectin order to use an object you MUST initialize it, meaning to give him a "content"there are 2 kinds of initializations; static one (like with the calendar class), or non-static one (with the "new *()" operator, * being a classname)
calvino_inda at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 4
Read the docs. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
jverda at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 5

> what does (= Calendar.getInstance()) do?

The API tells what it does. Read it.

I'm sorry for being so rude, but I mean, come on. This last question is "What does this method of this class do?" Well, if you know the name of the Class, and you know the name of the method, I would have to believe that it would not take more than about 20 seconds to look it up in the API.

Edit: And man, am I slow!

masijade.a at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 6
I'm a bit confused on how I am suppose to use the "Calendar.getInstance();"I read the API, but I don't know how it can help me out with the problem. I have tried many things with the code, but I still get the same error.
vopoa at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> I'm a bit confused on how I am suppose to use the

> "Calendar.getInstance();"

I don't know how to answer that other than Calendar cal = Calendar.getInstance();

What do you mean "how"?

> I read the API, but I don't know how it can help me

> out with the problem. I have tried many things with

> the code, but I still get the same error.

Without seeing your exact code, the exact error, and a clear explanation of what you're trying to accomplish, it's hard to provide adivce.

jverda at 2007-7-12 19:19:49 > top of Java-index,Java Essentials,Java Programming...
# 8

The error message that I recieve is:

"The local variable date may not have been initialized"

I don't know whats wrong with the date variable.

What I'm trying to do in my code is trying to create code to display dates in these 2 forms:

14-6-2007

June 14, 2007

In the Reformat method, I converted the months into strings, and in the storage method I want to make the Strings to display in this form:

June 14, 2007

Here's my code:

package util;

import java.util.Calendar;

import java.util.GregorianCalendar;

import labs.Lab2;

public class DateUtil

{

Calendar calendar = Calendar.getInstance();

GregorianCalendar greg;

public DateUtil()

{

}

public void GregorianCalendar(int year, int month, int date)

{

System.out.println("DATE: " + calendar.get(Calendar.DATE) + "-" +

calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.YEAR));

}

public String Reformat()

{

String jan = "Jan";

String feb = "Feb";

String march = "Mar";

if(calendar.MONTH > 0)

{

if(calendar.MONTH == 1)

return jan;

if(calendar.MONTH == 2)

return feb;

if(calendar.MONTH == 3)

return march;

}

return null;

}

public String Storage()

{

DateUtil date;

String nString;

nString = date.Reformat();

return null;

}

}

vopoa at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 9

public String Storage()

{

DateUtil date;

String nString;

nString = date.Reformat();

return null;

}

What value does date have above? What object does it point to? Answer: Undefined.

You need to give date a value. For instance, something like DateUtil date = new DateUtil();

jverda at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 10
<<< bets 10 bucks that he's gonna ask why it says "the variable may not have been initialized" when he's going to use "greg" var :D
calvino_inda at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 11
Thank youBut I have one more question about the calendar class.I noticed as I was reading the API, I saw the names of the months such as June.Is MONTH different from JUNE?like in my code:if(calendar.MONTH == 1)return jan;
vopoa at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 12
month variables in Calendar API allows you to use them when you set or get the month of your calendarit means that you can do: calendar.set(Calendar.MONTH, Calendar.JUNE), or: if (calendar.get(Calendar.MONTH) == Calendar.JUNE) // do something
calvino_inda at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 13

> month variables in Calendar API allows you to use

> them when you set or get the month of your calendar

>

> it means that you can do:

> calendar.set(Calendar.MONTH, Calendar.JUNE),

> or: if (calendar.get(Calendar.MONTH) ==

> Calendar.JUNE) // do something

the problem with my code right now is that its not outputting the current month such as june.

I added all the months already with my current code, but I get this output:

"Feb".

I tried the if statement:s like this, but they don't output the correct month.

Is there something missing in the code to grab the correct date? I have similiar code to this in a System.out.println(); and it gets the current date.

String june = "Jun";

if(calendar.get(Calendar.MONTH)==Calendar.JUNE)

return june;

-but I get an exception error

Message was edited by:

vopo

vopoa at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 14
If you are trying to go from a Date/Calendar to month string like "Jun",I strongly, STRONGLY suggest you look into SimpleDateFormat: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Hippolytea at 2007-7-12 19:19:50 > top of Java-index,Java Essentials,Java Programming...
# 15

Arrite I'll try the SimpleDateFormat:

but I have one problem with my current code. it says that I have:

"Exception in thread "main" java.lang.NullPointerException"

What does this message mean, I think it has to do with my nulls but I don't why?

I read the API but it didn't help me much.

From the last code I had:

String june = "Jun";

String july = "Jul";

if(calendar.get(Calendar.MONTH) == Calendar.JUNE)

return june;

if(calendar.get(Calendar.MONTH) == Calendar.JULY)

return july;

return null;

it gave me this error message

String june = "Jun";

String july = "Jul";

if(calendar.MONTH == 6)

return june;

if(calendar.MONTH == 7)

return july;

return null;

and the previous code I had didn't have this error, is there a reason why?

Message was edited by:

vopo

vopoa at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 16
Sounds like calendar is null.
Hippolytea at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 17
What line gives the NPE? In the code you show here, it looks like calendar must be null again. You should be able to diagnose and fix that by now.What exactly are you trying to accopmlish here? Your explanations aren't making any sense. Back up and explain what you're trying to
jverda at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 18
> and the previous code I had didn't have this error,> is there a reason why?No, there's no reason. The JVM just randomly throws out NPEs on a whim.
jverda at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 19
> No, there's no reason. The JVM just randomly throws> out NPEs on a whim.I am having the same problem. JVM randomly throws NPEs. Sometimes they hit me in the face but sometimes I am able to dodge them. Can you please give me some code for this problem?
DarumAa at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 20
> No, there's no reason. The JVM just randomly throws> out NPEs on a whim.I've set up a small shrine on my computer tower and that has helped.
Hippolytea at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 21
> I've set up a small shrine on my computer tower and> that has helped.I am moving my work-space to a temple!
DarumAa at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 22
> > I've set up a small shrine on my computer tower and> > that has helped.> > I am moving my work-space to a temple!Y'all put your hands on top of your monitors, I'm sending out a blessing:Satan be gone! NullPointerExceptions be
Hippolytea at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 23
> Y'all put your hands on top of your monitors, I'm> sending out a blessing:> Satan be gone! NullPointerExceptions be gone!I think its working!!!Oh no! ****! Satan wins again!Shucks...
DarumAa at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 24

Here's my code:

public String Reformat()

{

//simple = new SimpleDateFormat(MMM.d, y);

String jan = "Jan";

String feb = "Feb";

String march = "Mar";

String april = "Apr";

String may = "May";

String june = "Jun";

String july = "Jul";

String aug = "Aug";

String sept = "Sept";

String oct = "Oct";

String nov = "Nov";

String dec = "Dec";

if(calendar.get(Calendar.MONTH) == Calendar.JANUARY)

return jan;

if(calendar.get(Calendar.MONTH) == Calendar.FEBRUARY)

return feb;

if(calendar.get(Calendar.MONTH) == Calendar.MARCH)

return march;

if(calendar.get(Calendar.MONTH) == Calendar.APRIL)

return april;

if(calendar.get(Calendar.MONTH) == Calendar.MAY)

return may;

if(calendar.get(Calendar.MONTH) == Calendar.JUNE)

return june;

if(calendar.get(Calendar.MONTH) == Calendar.JULY)

return july;

if(calendar.get(Calendar.MONTH) == Calendar.AUGUST)

return aug;

if(calendar.get(Calendar.MONTH) == Calendar.SEPTEMBER)

return sept;

if(calendar.get(Calendar.MONTH) == Calendar.OCTOBER)

return oct;

if(calendar.get(Calendar.MONTH) == Calendar.NOVEMBER)

return nov;

if(calendar.get(Calendar.MONTH) == Calendar.DECEMBER)

return dec;

return null;

}

problem on the first if statement

vopoa at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 25
> problem on the first if statement what problem then?
OnBringera at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 26
Like I said before:1) You should be able to figure out where the NPE is coming from.2) I have no idea what you're trying to do.
jverda at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 27
> > problem on the first if statement > > what problem then?Not going for SimpleDateFormat?
Hippolytea at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 28
> > > problem on the first if statement > > > > what problem then?> > Not going for SimpleDateFormat?are you trying to answer every question with a better question?
OnBringera at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 29

> > > > problem on the first if statement

> > >

> > > what problem then?

> >

> > Not going for SimpleDateFormat?

>

> are you trying to answer every question with a better

> question?

Are you?

jverda at 2007-7-21 22:18:19 > top of Java-index,Java Essentials,Java Programming...
# 30

> > > > > problem on the first if statement

> > > >

> > > > what problem then?

> > >

> > > Not going for SimpleDateFormat?

> >

> > are you trying to answer every question with a better question?

>

> Are you?

And I thought that I've have to search and search to find a passive-aggressive support group ;-)

Hippolytea at 2007-7-21 22:18:24 > top of Java-index,Java Essentials,Java Programming...
# 31

> > > > > problem on the first if statement

> > > >

> > > > what problem then?

> > >

> > > Not going for SimpleDateFormat?

> >

> > are you trying to answer every question with a

> better

> > question?

>

> Are you?

don't you think it's a good way to get people to think?

OnBringera at 2007-7-21 22:18:24 > top of Java-index,Java Essentials,Java Programming...
# 32

> > > > > > problem on the first if statement

> > > > >

> > > > > what problem then?

> > > >

> > > > Not going for SimpleDateFormat?

> > >

> > > are you trying to answer every question with a better

> > > question?

> >

> > Are you?

>

> don't you think it's a good way to get people to think?

Now you're just gilding the rose...

Hippolytea at 2007-7-21 22:18:24 > top of Java-index,Java Essentials,Java Programming...
# 33
I figured out my problem, the calendar variable was not initialized.I realized that the variable was null and wasn't empty like I had expected.
vopoa at 2007-7-21 22:18:24 > top of Java-index,Java Essentials,Java Programming...