I am making an app which shows a line graph in the MainActiviy. The top button, directs to another activity. When I made the graph the, the second activity only contained a TextView, and the app was running.
Afer, I wanted to implement a system in which user can add data which will be saved in a SQLite database in this other activity. I followed the tutorials 49 to 54 from thenewboston.
I (believe) I did the same as in the tutorials. Nevertheless, my app is not running and it gives this error:
FATAL EXCEPTION: main. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
The full error can be be found below. Can someone help me to fix the error?
Error message
FATAL EXCEPTION: main Process: com.example.shanna.linechartgenerator, PID: 27077 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shanna.linechartgenerator/com.example.shanna.linechartgenerator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5272) 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:909) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184) at com.example.shanna.linechartgenerator.MainActivity.(MainActivity.java:18) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1572) at android.app.Instrumentation.newActivity(Instrumentation.java:1065) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5272) 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:909) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
MainActivity.Java:
package com.example.shanna.linechartgenerator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//public static final int ThresholdValue = 1;
LineChart lineChart = (LineChart) findViewById(R.id.chart);
//float newThreshold;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(2f, 3));
entries.add(new Entry(18f, 4));
entries.add(new Entry(9f, 5));
LineDataSet dataset = new LineDataSet(entries, "# of Calls");
// creating labels
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
LineData data = new LineData(labels, dataset);
lineChart.setData(data); // set the data and list of lables into chart
}
public void goToActivityOne(View view){
Intent intent = new Intent(this,ActivityOne.class);
startActivity(intent); //go to other activity in app
}
public void goToOtherApp(View view){
Intent intent2 = new Intent("HelloWorld_MakeValue");
//intent2.putExtra("newThreshold", newThreshold);
startActivity(intent2); //To go to activity in the other app
//startActivityForResult(intent2, ThresholdValue);
}
}
ActivityOne.java (The Activity with the input to save in the database)
package com.example.shanna.linechartgenerator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ActivityOne extends AppCompatActivity {
private EditText userInput;
private TextView userText;
private MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_layout);
userInput = (EditText) findViewById(R.id.userInput);
userText = (TextView) findViewById(R.id.userText);
dbHandler = new MyDBHandler(this, null, null, 1); //has 4 paramethers: context, DATABASE_NAME, factory, DATABASE_VERSION, see class
//method for add button, see below
printDataBase();
//method for the delete button, see below
//method for printing, see below
}
public void addButtonClicked(View view){
Painscales painscales = new Painscales(userInput.getText().toString());
dbHandler.addPainscale(painscales);
printDataBase();
//whenever user clicks the add button, take the input, add it to the database and print in below
}
public void printDataBase(){
String dbString = dbHandler.databaseToSting(); //get sting we retreived
userText.setText(dbString); //store it in here
userInput.setText(" "); //take input and set text to 'refresh' the input
}
public void deleteButtonClicked(View view){
String inputText = userInput.getText().toString();
dbHandler.deletePainscale(inputText);
printDataBase();
}
}
The MyDBHandler class
package com.example.shanna.linechartgenerator;
/**
* Created by Shanna on 1-6-2016.
* clas to work with the database
* with the help of the youtube tutorial, "Android App Development for Beginners - 51, 52 & 53 " from thenewboston
*/
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
public class MyDBHandler extends SQLiteOpenHelper{
//name that database
//column name etx
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "painscale.db";
public static final String TABLE_PAINSCALE = "painscale";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PAINSCALENAME = "painscaleName";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
} //constructor to pass info to class that works direclu with android (myDGHandler)
//on first call it needs to know what you are going to do, which is creating a table
@Override
public void onCreate(SQLiteDatabase db) {
//specify the table
String query = "CREATE_TABLE" + TABLE_PAINSCALE + "(" +
COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
COLUMN_PAINSCALENAME + "TEXT" + ")";
db.execSQL(query); //create the table
}
//to update the table
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_PAINSCALE); //delete the current table
onCreate(db); //create new updated table
}
//Add a new row to the database
public void addPainscale(Painscales painscales){
ContentValues values = new ContentValues();
values.put(COLUMN_PAINSCALENAME, painscales.get_painscaleName());
SQLiteDatabase db = getWritableDatabase(); //key to the
db.insert(TABLE_PAINSCALE, null, values); //inset new row into table
db.close();//close database, we are done
}
//Delete painscale from the database
public void deletePainscale(String painscaleName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM" + TABLE_PAINSCALE + "WHERE" + COLUMN_PAINSCALENAME + "=\"" +painscaleName + "=\";" ); //delete where productname is same as the input
}
//print out the database, as a string
public String databaseToSting(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT + FROM " + TABLE_PAINSCALE + "WHERE 1";
//cursor point at location in results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("painscaleName"))!= null){
dbString += c.getString(c.getColumnIndex("painscaleName"));
dbString += "\n";
} //loops throuhg all painsclase names, everytime it does, it would place a new one on a new row. Is needed so thevalesarenotwritenlikethis
}
db.close();
return dbString;
}
}
The Painscales.java class
package com.example.shanna.linechartgenerator;
/**
* Created by Shanna on 1-6-2016.
* class to deal with the user input
* with the help of the youtube tutorial, "Android App Development for Beginners - 49 and 50, from thenewboston
*/
public class Painscales {
//java needs an id number and the pain scale
private int _id;
private String _painscaleName;
public Painscales(){
}
public Painscales(String painscaleName) {
this._painscaleName = painscaleName; //give input data automatically string to whatever user typed in
}
public void set_id(int _id) {
this._id = _id; //give input an id
}
public void set_painscaleName(String _painscaleName) {
this._painscaleName = _painscaleName; //set input as a name
}
public int get_id() {
return _id; //get input id
}
public String get_painscaleName() {
return _painscaleName; //get input name
}
}