我创建的程序以“2000年1月7日”格式输入日期时计算星期几。当我点击按钮计算星期几时,为什么会出现NullPointerException
异常?
以下是app的源代码:
public class MainActivity extends AppCompatActivity
{
private int position;
private String value;
private Button button;
private EditText editText;
private TextView textView1,textView2;
private Spinner spinner_month,spinner_day;
private ArrayAdapter<CharSequence> adapter;
private boolean isValid;
private Date date;
private int inMonth, inDay, inYear;
static boolean isDateValid(int month, int day, int year)
{
boolean validation = true;
int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month < 1 || month > 12)
{
validation = false;
}
if (day < 1 || day > daysInMonth[month - 1])
{
validation = false;
}
if (year < 1700 || year > 3000)
{
validation = false;
}
return validation;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button = (Button) findViewById(R.id.button_calculate);
textView1 = (TextView) findViewById(R.id.textView_day);
textView2 = (TextView) findViewById(R.id.textView_year);
spinner_month = (Spinner) findViewById(R.id.spinner_month);
spinner_day = (Spinner) findViewById(R.id.spinner_day);
editText = (EditText) findViewById(R.id.editText_year);
adapter = ArrayAdapter.createFromResource(this,R.array.months,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_month.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(this,R.array.day,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_day.setAdapter(adapter);
if (spinner_month.getSelectedItemPosition() == 0)
value = "1";
if (spinner_month.getSelectedItemPosition() == 1)
value = "2";
if (spinner_month.getSelectedItemPosition() == 2)
value = "3";
if (spinner_month.getSelectedItemPosition() == 3)
value = "4";
if (spinner_month.getSelectedItemPosition() == 4)
value = "5";
if (spinner_month.getSelectedItemPosition() == 5)
value = "6";
if (spinner_month.getSelectedItemPosition() == 6)
value = "7";
if (spinner_month.getSelectedItemPosition() == 7)
value = "8";
if (spinner_month.getSelectedItemPosition() == 8)
value = "9";
if (spinner_month.getSelectedItemPosition() == 9)
value = "10";
if (spinner_month.getSelectedItemPosition() == 10)
value = "11";
if (spinner_month.getSelectedItemPosition() == 11)
value = "12";
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String dayOfWeek;
boolean isValid;
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String strDate = sdf.format(c.getTime());
do
{
try
{
String getdate = editText.getText().toString()+ value +
spinner_day.getItemAtPosition(position).toString();
date = sdf.parse(getdate);
}
catch (ParseException ex)
{
Toast.makeText(MainActivity.this,ex.getMessage(),Toast.LENGTH_SHORT);
}
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
if (isValid == false)
{
Toast.makeText(MainActivity.this, "Uneli ste pogresan datum", Toast.LENGTH_SHORT).show();
}
}
while (isValid == false);
dayOfWeek = zellerCalc(date.getMonth(), date.getDay(), date.getYear());
textView1.setText(dayOfWeek);
}
});
}
logcat的:
02-07 12:32:16.349 3049-3049 / com.example.pavle.dayofborn E / AndroidRuntime:致命异常:主要 过程:com.example.pavle.dayofborn,PID:3049 显示java.lang.NullPointerException 在 com.example.pavle.dayofborn.MainActivity $ 2.onClick(MainActivity.java:230) 在android.view.View.performClick(View.java:4633) 在android.view.View $ PerformClick.run(View.java:19270) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本地方法)
//Line 230: isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
答案 0 :(得分:1)
你的问题是范围。
你有一个实例变量
private Date date;
但你在try catch
中声明了另一个try
{
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
Date date = sdf.parse(getdate);
}
catch (ParseException ex)
{
}
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
因此,一旦你退出try catch,其中声明的那个(并且初始化)不存在,实例变量永远不会被声明,所以留在null
。
您希望初始化实例1,因此请删除声明,如下所示:
date = sdf.parse(getdate);
PS:因为异常会跳过这个问题,你最终会遇到同样问题,你应该把isValid
放在try catch中
boolean isValid = false;
...
try
{
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
date = sdf.parse(getdate);
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
if (isValid == false)
{
Toast.makeText(MainActivity.this, "Uneli ste pogresan datum", Toast.LENGTH_SHORT).show();
}
}
catch (ParseException ex)
{
}
注意:请注意,Date
getter几乎全部已弃用,您应该使用Calendar.setTime(Date date)
来Calendar.get(int field)
安全使用Date
。然后,在验证中,检查您收到的值,日期不是1/1/0000的日/月/年数。月份和日期基于0,因此ask drivers
[
ifelse temperature < 5
[set shape "car"]
[ move-drivers ]
ifelse temperature > 5
[set shape "bike"]
[ move-drivers ]
]
答案 1 :(得分:0)
我认为你没有初始化变量日期。
试试这个
$query = <<<EOF
LOAD DATA LOCAL INFILE '$file'
INTO TABLE users
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(name,mobile,email)
EOF;
祝你好运。