Search code examples
javaandroidandroid-studioopencsv

Attempt to invoke virtual method on a null object reference (Error when adding strings)


I'm trying to build a simple app that takes in data and outputs it as a CSV.

Everything is working, but now I am at the point where I am outputting the CSV. When I add the string declarations, I get this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

But when the debugger names my MainActivity it references the line my strings begin on. I really have no idea what I'm doing wrong and am admittedly very new to this.

Here is my code:

package com.tscmusa.pkelley.gpsform;

import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ViewFlipper;
import android.view.MotionEvent;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private ViewFlipper viewFlipper;
    private float lastX;


// File exist


    EditText sub;
    EditText casenumber;
    EditText customer;
    EditText date;
    EditText po;

    String fileName = (sub + "," + date);

    Button btnOk;
    Button btnReturn;

 /*
     String sub = ((EditText)findViewById(R.id.txtSubject)).getText().toString();
    String casenumber = ((EditText)findViewById(R.id.txtCaseNumber)).getText().toString();
    String customer = ((EditText)findViewById(R.id.txtCustomer)).getText().toString();
    String date = ((EditText)findViewById(R.id.txtDate)).getText().toString();
    String po = ((EditText)findViewById(R.id.txtPO)).getText().toString();
    String plate1 = ((EditText)findViewById(R.id.txtPlate1)).getText().toString();
    String boxnumber1 = ((EditText)findViewById(R.id.txtBox1)).getText().toString();
    String label1 = ((EditText)findViewById(R.id.txtLabel1)).getText().toString();
    String make1 = ((EditText)findViewById(R.id.txtMake1)).getText().toString();
    String model1 = ((EditText)findViewById(R.id.txtModel1)).getText().toString();
    String year1 = ((EditText)findViewById(R.id.txtYear1)).getText().toString();
    String odo1 = ((EditText)findViewById(R.id.txtODO1)).getText().toString();
    String vin1 = ((EditText)findViewById(R.id.txtVIN1)).getText().toString();
    String notes1 = ((EditText)findViewById(R.id.txtNotes1)).getText().toString();
    String plate2 = ((EditText)findViewById(R.id.txtPlate2)).getText().toString();
    String boxnumber2 = ((EditText)findViewById(R.id.txtBox2)).getText().toString();
    String label2 = ((EditText)findViewById(R.id.txtLabel2)).getText().toString();
    String make2 = ((EditText)findViewById(R.id.txtMake2)).getText().toString();
    String model2 = ((EditText)findViewById(R.id.txtModel2)).getText().toString();
    String year2 = ((EditText)findViewById(R.id.txtYear2)).getText().toString();
    String odo2 = ((EditText)findViewById(R.id.txtODO2)).getText().toString();
    String vin2 = ((EditText)findViewById(R.id.txtVIN2)).getText().toString();
    String notes2 = ((EditText)findViewById(R.id.txtNotes2)).getText().toString();
    String plate3 = ((EditText)findViewById(R.id.txtPlate3)).getText().toString();
    String boxnumber3 = ((EditText)findViewById(R.id.txtBox3)).getText().toString();
    String label3 = ((EditText)findViewById(R.id.txtLabel3)).getText().toString();
    String make3 = ((EditText)findViewById(R.id.txtMake3)).getText().toString();
    String model3 = ((EditText)findViewById(R.id.txtModel3)).getText().toString();
    String year3 = ((EditText)findViewById(R.id.txtYear3)).getText().toString();
    String odo3 = ((EditText)findViewById(R.id.txtODO3)).getText().toString();
    String vin3 = ((EditText)findViewById(R.id.txtVIN3)).getText().toString();
    String notes3 = ((EditText)findViewById(R.id.txtNotes3)).getText().toString();
    String plate4 = ((EditText)findViewById(R.id.txtPlate4)).getText().toString();
    String boxnumber4 = ((EditText)findViewById(R.id.txtBox4)).getText().toString();
    String label4 = ((EditText)findViewById(R.id.txtLabel4)).getText().toString();
    String make4 = ((EditText)findViewById(R.id.txtMake4)).getText().toString();
    String model4 = ((EditText)findViewById(R.id.txtModel4)).getText().toString();
    String year4 = ((EditText)findViewById(R.id.txtYear4)).getText().toString();
    String odo4 = ((EditText)findViewById(R.id.txtODO4)).getText().toString();
    String vin4 = ((EditText)findViewById(R.id.txtVIN4)).getText().toString();
    String notes4 = ((EditText)findViewById(R.id.txtNotes4)).getText().toString();
    String plate5 = ((EditText)findViewById(R.id.txtPlate5)).getText().toString();
    String boxnumber5 = ((EditText)findViewById(R.id.txtBox5)).getText().toString();
    String label5 = ((EditText)findViewById(R.id.txtLabel5)).getText().toString();
    String make5 = ((EditText)findViewById(R.id.txtMake5)).getText().toString();
    String model5 = ((EditText)findViewById(R.id.txtModel5)).getText().toString();
    String year5 = ((EditText)findViewById(R.id.txtYear5)).getText().toString();
    String odo5 = ((EditText)findViewById(R.id.txtODO5)).getText().toString();
    String vin5 = ((EditText)findViewById(R.id.txtVIN5)).getText().toString();
    String notes5 = ((EditText)findViewById(R.id.txtNotes5)).getText().toString();
    String plate6 = ((EditText)findViewById(R.id.txtPlate5)).getText().toString();
    String boxnumber6 = ((EditText)findViewById(R.id.txtBox5)).getText().toString();
    String label6 = ((EditText)findViewById(R.id.txtLabel5)).getText().toString();
    String make6 = ((EditText)findViewById(R.id.txtMake5)).getText().toString();
    String model6 = ((EditText)findViewById(R.id.txtModel5)).getText().toString();
    String year6 = ((EditText)findViewById(R.id.txtYear5)).getText().toString();
    String odo6 = ((EditText)findViewById(R.id.txtODO5)).getText().toString();
    String vin6 = ((EditText)findViewById(R.id.txtVIN5)).getText().toString();
    String notes6 = ((EditText)findViewById(R.id.txtNotes5)).getText().toString();
    String subject = (date + (" ") + sub);
    String fileName = (subject);

*/




    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

        sub = (EditText) findViewById(R.id.txtSubject);
        casenumber = (EditText) findViewById(R.id.txtCaseNumber);
        customer = (EditText) findViewById(R.id.txtCustomer);
        date = (EditText) findViewById(R.id.txtDate);
        po = (EditText) findViewById(R.id.txtPO);

        Intent p1 = getIntent();
       final String plate1 = p1.getStringExtra("pl1");

       Intent bn1 = getIntent();
      final String boxnumber1 = bn1.getStringExtra("bon1");

      Intent lb1 = getIntent();
        final String label1 = lb1.getStringExtra("lbl1");

        Intent ma1 = getIntent();
        final String make1 = ma1.getStringExtra("mak1");

        Intent mo1 = getIntent();
        final String model1 = mo1.getStringExtra("mod1");

        Intent y1 = getIntent();
        final String year1 = y1.getStringExtra("ye1");

        Intent o1 = getIntent();
        final String odo1 = o1.getStringExtra("od1");

        Intent v1 = getIntent();
        final String vin1 = v1.getStringExtra("vi1");

        Intent n1 = getIntent();
        final String notes1 = n1.getStringExtra("no1");

        Button btnOk = (Button) findViewById(R.id.btnOK);
        Button btnReturn = (Button) findViewById(R.id.btnReturn);

        btnReturn.setOnClickListener(new View.OnClickListener()
                                     {
                                         public void onClick(View v) {
                                             viewFlipper.showPrevious();
                                         }
                                     }
        );

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String content = "Technician" + "," + "Case #" + "," + "Customer" + "," + "Date" + "," + "PO" + "/n"
                            + sub.getText().toString() + "," + casenumber.getText().toString() + "," + customer.getText().toString() + "," + date.getText().toString() + "," + po.getText().toString() + "/n" +
                            "Plate" + "," + "Box Number" + "," + "Label" + "," + "Make" + "Model" + "," + "Year" + "," + "ODO" + "," + "VIN" + "," + "Notes" + "/n"
                            + plate1 + "," + boxnumber1 + "," + label1 + "," + make1 + "," + model1 + "," + year1 + "," + odo1 + "," + vin1 + "," + notes1 + "/n";

                    File file = new File(fileName +".csv");
                    /* if file doesnt exists, then create it */
                    if (!file.exists()) {
                        file.createNewFile();
                    }

                    FileWriter fw = new FileWriter(file.getAbsoluteFile());
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(content);
                    bw.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });



        if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }




        EditText txtDate = (EditText) findViewById( R.id.txtDate );
        SimpleDateFormat sdf = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            sdf = new SimpleDateFormat( "yyyy/MM/dd" );
        }
        txtDate.setText( sdf.format( new Date() ));

        String[] deviceList = {"Device 1", "Device 2", "Device 3", "Device 4", "Device 5", "Device 6", "Device 7", "Device 8", "Device9", "Device 10", "Device 11", "Device 12", "Device 13", "Device 14", "Device 15"};

        ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, deviceList);

        ListView theListView = (ListView) findViewById(R.id.theListView);
        theListView.setAdapter(theAdapter);


        theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (position==0)
                {
                    Intent intent = new Intent(MainActivity.this,Device1.class);
                    startActivity(intent);
                }
                if (position==1)
                {
                    Intent intent = new Intent(MainActivity.this,Device2.class);
                    startActivity(intent);
                }
                if (position==2)
                {
                    Intent intent = new Intent(MainActivity.this,Device3.class);
                    startActivity(intent);
                }
                if (position==3)
                {
                    Intent intent = new Intent(MainActivity.this,Device4.class);
                    startActivity(intent);
                }
                if (position==4)
                {
                    Intent intent = new Intent(MainActivity.this,Device5.class);
                    startActivity(intent);
                }
                if (position==5)
                {
                    Intent intent = new Intent(MainActivity.this,Device6.class);
                    startActivity(intent);
                }
                if (position==6)
                {
                    Intent intent = new Intent(MainActivity.this,Device7.class);
                    startActivity(intent);
                }
                if (position==7)
                {
                    Intent intent = new Intent(MainActivity.this,Device8.class);
                    startActivity(intent);
                }
                if (position==8)
                {
                    Intent intent = new Intent(MainActivity.this,Device9.class);
                    startActivity(intent);
                }
                if (position==9)
                {
                    Intent intent = new Intent(MainActivity.this,Device10.class);
                    startActivity(intent);
                }
                if (position==10)
                {
                    Intent intent = new Intent(MainActivity.this,Device11.class);
                    startActivity(intent);
                }
                if (position==11)
                {
                    Intent intent = new Intent(MainActivity.this,Device12.class);
                    startActivity(intent);
                }
                if (position==12)
                {
                    Intent intent = new Intent(MainActivity.this,Device13.class);
                    startActivity(intent);
                }
                if (position==13)
                {
                    Intent intent = new Intent(MainActivity.this,Device14.class);
                    startActivity(intent);
                }
                if (position==14)
                {
                    Intent intent = new Intent(MainActivity.this,Device15.class);
                    startActivity(intent);
                }

            }
        });
    }
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {

            case MotionEvent.ACTION_DOWN:
                lastX = touchevent.getX();
                break;
            case MotionEvent.ACTION_UP:
                float currentX = touchevent.getX();

                // Handling left to right screen swap.
                if (lastX < currentX) {

                    // If there aren't any other children, just break.
                    if (viewFlipper.getDisplayedChild() == 0)
                        break;

                    // Next screen comes in from left.
                    viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
                    // Current screen goes out from right.
                    viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

                    // Display next screen.
                    viewFlipper.showNext();
                }

                // Handling right to left screen swap.
                if (lastX > currentX) {

                    // If there is a child (to the left), kust break.
                    if (viewFlipper.getDisplayedChild() == 1)
                        break;

                    // Next screen comes in from right.
                    viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
                    // Current screen goes out from left.
                    viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

                    // Display previous screen.
                    viewFlipper.showPrevious();
                }
                break;
        }
        return false;
    }








}

I've been trying to make this work by transferring the strings from my other classes but to no avail. Here is the example from Device1.class:

package com.tscmusa.pkelley.gpsform;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Device1 extends AppCompatActivity {

    public void BacktoMain(View view)
    {
        Intent intent = new Intent(Device1.this, MainActivity.class);
        startActivity(intent);

    }

    EditText plate1;
    EditText boxnumber1;
    EditText label1;
    EditText make1;
    EditText model1;
    EditText year1;
    EditText odo1;
    EditText vin1;
    EditText notes1;

    private boolean mMyBoolean;

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean("MyBoolean", mMyBoolean);
        // ... save more data
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mMyBoolean = savedInstanceState.getBoolean("MyBoolean");
        // ... recover more data
    }

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

        plate1 = (EditText) findViewById(R.id.txtPO);
        Intent p1 = new Intent(this, MainActivity.class);
        p1.putExtra("pl1", plate1.getText().toString());
        startActivity(p1);

        boxnumber1 = (EditText) findViewById(R.id.txtBox1);
        Intent bn1 = new Intent(this, MainActivity.class);
        bn1.putExtra("bon1", boxnumber1.getText().toString());
        startActivity(bn1);

        label1 = (EditText) findViewById(R.id.txtLabel1);
        Intent lb1 = new Intent(this, MainActivity.class);
        lb1.putExtra("lbl1", label1.getText().toString());
        startActivity(lb1);

        make1 = (EditText) findViewById(R.id.txtMake1);
        Intent ma1 = new Intent(this, MainActivity.class);
        ma1.putExtra("mak1", make1.getText().toString());
        startActivity(ma1);

        model1 = (EditText) findViewById(R.id.txtModel1);
        Intent mo1 = new Intent(this, MainActivity.class);
        mo1.putExtra("mod1", model1.getText().toString());
        startActivity(mo1);

        year1 = (EditText) findViewById(R.id.txtYear1);
        Intent y1 = new Intent(this, MainActivity.class);
        y1.putExtra("ye1", year1.getText().toString());
        startActivity(y1);

        odo1 = (EditText) findViewById(R.id.txtODO1);
        Intent o1 = new Intent(this, MainActivity.class);
        o1.putExtra("od1", odo1.getText().toString());
        startActivity(o1);

        vin1 = (EditText) findViewById(R.id.txtVIN1);
        Intent v1 = new Intent(this, MainActivity.class);
        v1.putExtra("vi1", vin1.getText().toString());
        startActivity(v1);

        notes1 = (EditText) findViewById(R.id.txtNotes1);
        Intent n1 = new Intent(this, MainActivity.class);
        n1.putExtra("no1", notes1.getText().toString());
        startActivity(n1);

        if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
    }

}

Any help would be very greatly appreciated.


Solution

  • You cannot have this initialization here:

    String vin1 = ((EditText)findViewById(R.id.txtVIN1)).getText().toString();
    

    You can declare your variable there like: EditText vin1;

    And then in onCreate AFTER calling setContentView put the initialization like:

    vin1 = (EditText) findViewById(R.id.txtVIN1);
    

    Then when you need call vin1.getText().toString();