无法解决java.lang.NullPointerException的解决方案

时间:2018-11-19 20:25:59

标签: java nullpointerexception

我是我的第一门编程课的学生,并且已经广泛研究了此错误,但是所提供的答案均与我的情况无关。该程序用于分配作业;我们的任务是创建一个星座程序,根据用户输入的月份和日期给出星座,符号和符号。

当我尝试运行程序时,我的getHoroscope方法出现NullPointerException错误,该方法使用switch语句根据符号确定星座。

这是我收到的错误:

Exception in thread "main" java.lang.NullPointerException
at SwamiBuckner.getHoroscope(SwamiBuckner.java:144)
at demoSwamiBuckner.main(demoSwamiBuckner.java:25)

这是我的getHoroscope方法:

public String getHoroscope()
{
  switch(sign)
  {
     case "Aries":
        horoscope = "Your energy is off the charts today! This almost ensures that you'll have a great day, thanks to your madcap approach to life.";
        break;
     case "Taurus":
        horoscope = "Your energy is up today. You're resourceful by nature, and now you're especially so. You've got the guts and the creativity to do just about anything you want -- so make it good!";
        break;
     case "Gemini":             
        horoscope = "You're in high spirits today, and your impulsive streak is a mile wide. Your friends and family know you well enough to know that when you're like this, they should just stand back, because you're a force of nature!";
        break;
     case "Cancer":
        horoscope = "You're strong of mind and heart today. If you're required to take charge of a group or situation, you should have little trouble -- except that other people are in touch with their inner authorities now, too.";
        break;
     case "Leo":
        horoscope = "Your creativity swells today. Maybe it's your high energy, or maybe it's just your willingness to try something new without worrying how it might end up.";
        break;
     case "Virgo":
        horoscope = "Your creativity swells today. Maybe it's your high energy, or maybe it's just your willingness to try something new without worrying how it might end up.";
        break;
     case "Libra":
        horoscope = "Some brash energy is flying around today. People are in unusually independent moods. You might wish everyone could just get along, but don't expect that to happen.";
        break;
     case "Scorpio":
        horoscope = "Your temper is set to simmer today, which means it could flare easily with just the smallest of sparks. What's more, you see little reason to hold yourself back from speaking your mind.";
        break;
     case "Sagittarius":
        horoscope = "Your individualist streak flares to life today. Whatever you're passionate about, use the current energy burst to pursue it ardently. Spirited efforts are favored now.";
        break;
     case "Capricorn":
        horoscope = "Watch how you speak to people today, especially your children, students, teammates or employees. The current cosmic energy makes you less sensitive than usual to others' feelings.";
        break;
     case "Aquarius":
        horoscope = "Today your interest in a person or subject is sparked. Suddenly, you're filled with ardent enthusiasm. Your blood is humming -- in your excitement, you might literally hear it rushing in your ears!";
        break;
     case "Pisces":
        horoscope = "Try not to be thin-skinned today. The current energy leaves no room for sensitivity. People are in bold, even aggressive moods, so they probably aren't looking out for your feelings.";
        break;
     default:
        System.out.println("getHoroscope error");
        break;
  }

  return horoscope;
}

该错误发生在switch语句的开头:switch(sign)。在驱动程序中,它指向我尝试调用getHoroscope方法的行:

System.out.println(name1 + ", Swami says: " + swam1.getHoroscope());

最后,这是将值分配给sign属性的方法:

public String getSign()
{
  switch(month)
  { 
     case 01:
        if (day >= 01 && day <= 19)
           sign = "Capricorn";
        else if (day >= 20 && day <= 31)
           sign = "Aquarius";
        break;

     case 02:
       if (day >= 01 && day <= 18)
           sign = "Aquarius";
       else if (day >= 19 && day <= 28)
           sign = "Pisces";
       break;

     case 03:
        if (day >= 01 && day <= 20)
           sign = "Pisces";
       else if (day >= 21 && day <= 31)
           sign = "Aries";              
        break;

     case 04:
        if (day >= 01 && day <= 19)
           sign = "Aries";
       else if (day >= 20 && day <= 30)
           sign = "Taurus";
        break;

     case 05:
        if (day >= 01 && day <= 20)
           sign = "Taurus";
       else if (day >= 21 && day <= 31)
           sign = "Gemini";
        break;

     case 06:
        if (day >= 01 && day <= 21)
           sign = "Gemini";
       else if (day >= 22 && day <= 30)
           sign = "Cancer";
        break;

     case 07:
        if (day >= 01 && day <= 22)
           sign = "Cancer";
       else if (day >= 23 && day <= 31)
           sign = "Leo";
        break;

     case 8: //fix this
        if (day >= 01 && day <= 22)
           sign = "Leo";
       else if (day >= 23 && day <= 31)
           sign = "Virgo";
        break;

     case 9: //fix this
        if (day >= 01 && day <= 22)
           sign = "Virgo";
       else if (day >= 23 && day <= 30)
           sign = "Libra";
        break;

     case 10:
        if (day >= 01 && day <= 23)
           sign = "Libra";
       else if (day >= 24 && day <= 31)
           sign = "Scorpio";
        break;

     case 11:
        if (day >= 01 && day <= 21)
           sign = "Scorpio";
       else if (day >= 22 && day <= 30)
           sign = "Sagittarius";
        break;

     case 12:
        if (day >= 01 && day <= 21)
           sign = "Sagittarius";
       else if (day >= 22 && day <= 31)
           sign = "Capricorn";
        break;

     default:
        System.out.println("getSign error");  
  }

  return sign;
}

我在做什么错?我的研究表明,当引用字段设置为null并随后代码尝试使用它们时,会发生此错误,但是我认为这些字段将在getSign方法中初始化。谢谢您的帮助!

0 个答案:

没有答案