I searched quite a bit and I'm very new to java so I'm trying to create a basic program that will do the following
User will insert birth date or horoscope sign
The program will then tell the user what their sign is or what range of dates that particular horoscope goes through.
I'm currently at the point where this line... " ourDate.getIndexByDate(myDay); " Gives a null pointer exception. At the same time, so does the method that ourDate is calling.
java.lang.NullPointerException
at Dates.getIndexByDate(Dates.java:68)
at Dates.main(Dates.java:49)
I have the following code:
import java.util.ArrayList;
import java.util.Scanner;
public class Dates {
private static ArrayList<Double> zodiacDates;
private static ArrayList<String> zodiac;
public static void main(String[] args) {
ArrayList<Double> zodiacDates = new ArrayList<Double>()
{{
add(1.19);
add(2.18);
add(3.20);
add(4.21);
add(5.20);
add(6.20);
add(7.22);
add(8.22);
add(9.22);
add(10.22);
add(11.21);
add(12.21);
}};
ArrayList<String> zodiac = new ArrayList<String>()
{{
add("Capricorn");
add("Aquarius");
add("Pisces");
add("Aries");
add("Taurus");
add("Gemini");
add("Cancer");
add("Leo");
add("Virgo");
add("Libra");
add("Scorpio");
add("Sagittarius");
}};
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your birthdate in the form of a double");
Double myDay = keyboard.nextDouble();
System.out.println("Enter your horoscope sign if you know it");
String ourSign = keyboard.nextLine();
Dates ourDate = new Dates();
System.out.println("Enter your birthdate in the form of a double");
ourDate.getIndexByDate(myDay);
ourDate.getIndexBySign(ourSign);
}
public int getIndexBySign(String pName)
{
for (int w = 0; w < zodiac.size(); w++)
{
String sign = zodiac.get(w);
if (pName.equals(sign))
{
return w;
}
}
return -1;
}
public int getIndexByDate(Double myDay)
{
for (int i = 0; i <= zodiacDates.size(); i++)
{
Double variable = zodiacDates.get(i);
if ((myDay <= variable && myDay > zodiacDates.get(i-1)) || (myDay < 1.19 || myDay >= 12.22 ) )
{
return i;
}
}
return -1;
}
}