运行应用程序时扫描程序null错误

时间:2016-11-18 18:09:28

标签: java android debugging runtime-error android-studio-2.2

我正在运行一个简单的Android应用程序。它是一个随机的“财富Cookie”,它要求输入姓名,姓氏,dob,当按下按钮时,从包含cookie的txt文件中读取,并随机输出一个。我首先使用system.out.println在控制台上运行应用程序。没问题。当我把它放在android mainactivity中。我按下按钮时出错了。该应用程序冻结。这是代码和错误 运行错误

W/art: Suspending all threads took: 53.890ms
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jorge.whatsyourname, PID: 3074
                  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Scanner.hasNextLine()' on a null object reference
                      at com.example.jorge.whatsyourname.MainActivity$1.onClick(MainActivity.java:69)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5257)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 3074 SIG: 9

package com.example.jorge.whatsyourname;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
    //variables declaration
    EditText nameTxt;
    EditText lastnameTxt;
    EditText dobTxt;
    TextView cookieOutTxt;

    int random = 0; //generated random number
    int max = 0; //value to be assign on execution (number of fortunes)
    ArrayList<String> fortunes; //all fortunes will be stored here
    Scanner in;
    File inputFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fortunes = new ArrayList<String>();

        inputFile = new File("/home/jorge/AndroidStudioProjects/Fortune Cookie" +
                "/app/src/main/res/fortuneCookie.txt");

        try {
            in = new Scanner(inputFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //references
        nameTxt = (EditText) findViewById(R.id.nameTxt);
        lastnameTxt = (EditText) findViewById(R.id.lastnameTxt);
        dobTxt = (EditText) findViewById(R.id.dobTxt);
        cookieOutTxt = (TextView) findViewById(R.id.cookieOutTxt);

        final Button cookieBtn = (Button) findViewById(R.id.cookieBtn);
        cookieBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //scan file if line contains % count, else add to fortune arraylist
                boolean flag = false;
                String phrase = "";
                while (in.hasNextLine()) {
                    String line = in.nextLine();
                    //System.out.println(line);
                    if (line.contains("%") && !flag) {
                        flag = true;
                    } else if (line.contains("%") && flag) {
                        max++;
                        phrase = "";
                    } else if (line.isEmpty()) {
                        ; //do nothing and continue
                    } else {
                        phrase = phrase + line;
                        fortunes.add(max, phrase);
                    }
                }
                //generating a random fortune
                random = (int) (Math.random() * max + 1);
                //printing output to screen
                cookieOutTxt.setText(fortunes.get(random));

            }
        });

    }


    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }
}

0 个答案:

没有答案