Encountering an issue in Android Studio 1.3.1 when trying to create a SQLite

I encountered an error on line 26 of MainActivity.java. A Java NULLPOINTER Exception is being displayed. Can someone please advise me on how to resolve this issue? My code is available on GitHub. https://github.com/happyshravan/SQLite

Latest LogCat error:

2-25 21:10:58.729  15661-15661/abcd.shravankr.sqlite D/memalloc﹕ /dev/pmemMapped buffer base:0x530b2000 size:10047488 offset:8511488 fd:66
12-25 21:11:06.679  15661-15661/abcd.shravankr.sqlite D/AndroidRuntime﹕ Shutting down VM
12-25 21:11:06.679  15661-15661/abcd.shravankr.sqlite W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40c15a68)
12-25 21:11:06.699  15661-15661/abcd.shravankr.sqlite E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
        at android.view.View.performClick(View.java:3571)
        at android.view.View$PerformClick.run(View.java:14247)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4517)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at 

Answer №1

I encountered an issue on line 26 of MainActivity.java

Line 26 pertains to the method

public void addButtonClicked(View view)

In your XML file, you have defined two buttons with ids addButton and deleteButton, but it seems like they are not being utilized in MainActivity.

A Java NullPointerException is being thrown

This is because you are trying to use these buttons without initializing them first.

Solution :

Try adding this code inside the onCreate method:

Button addBtn=(Button)findViewById(R.id.addButton);
Button deleteBtn=(Button)findViewById(R.id.deleteButton);

Edited

Remove

android:onClick="addButtonClicked"
and
android:onClick="deleteButtonClicked"
from the main_activity XML file, and remove addButtonClicked and deleteButtonClicked methods from MainActivity. Replace them with the following code.

MainActivity

 addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //   add button is clicked
            Toast.makeText(getApplicationContext(),"Shravan",Toast.LENGTH_LONG).show();
            Products product = new Products(shravansInput.getText().toString());
            dbHandler.addProduct(product);
            printDatabase();
            }
        });


  deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //   delete button is clicked
            String inputText=shravansInput.getText().toString();
            dbHandler.deleteProduct(inputText);
            printDatabase();
        });

Place this code inside the onCreate method.

I believe implementing these changes will resolve the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Iterating through a JSON object to verify the presence of a specific value

I have a JSON Object and I am looking for a way in Angular 6 to search for the value "Tennis" in the key "name". Can you provide guidance on how to achieve this? { "id":2, "name":"Sports", "url":"/sports" "children":[ { "id":1, ...

Tips for deleting dataset rows using column headers in c#

If I have a dataset with columns labeled A, B, and C, and I need to clear the contents of just column B, what is the best approach? So far, I have tried removing column B from the dataset: Dataset.Tables[0].Columns.Remove(B); Then adding it back in: Da ...

"Hidden panels in Sencha Touch only respond to show() and hide() methods after a resize event

Check out this demonstration of a Sencha Touch app by visiting this link. The button located in the bottom-left corner is supposed to show or hide the menu panel on top of the "Location info goes here" bar, but it seems to be functioning in an unexpected m ...

Optimizing load behavior in React using Node.js Express and SQL operations

As someone who is fairly new to programming, I have a question regarding the connection between server and client sides in applications like React and other JavaScript frameworks. Currently, I am working with a MySQL database where I expose a table as an ...

I am interested in utilizing $axios in conjunction with Vuex constants for my project

My Dream Becoming Reality I frequently use this.$axios, so I attempted to store it in a constant, but unfortunately, it did not work as expected. Despite reading the official documentation, I couldn't grasp the reason behind this issue. Could it be d ...

What is the process by which the new operator functions on exceptions constructor in the absence of a class declaration

Is it possible to implement a constructor without naming or requiring the class? For example, how can I simply write new string() without assigning it to a variable? I understand that this calls the constructor, but isn't the class supposed to be in ...

Tips on showing an api call response in Reactjs

I am a beginner in Reactjs and currently working with nextjs. I have been trying to integrate a "newsletter" feature into my project. I have created a component for this which is functioning properly. However, I am facing an issue with displaying the "succ ...

Is there a way to preserve line breaks when programmatically copying text to the clipboard with JavaScript?

Utilizing Javascript alongside a duo of devexpress asp.net controls to dynamically duplicate the contents of an ASPxMemo (multiline textfield) and assign those contents as the body of an email. The copying and pasting is functioning correctly for the most ...

Troubleshooting: Issues with updating a text field in Vue test utils using Jest

Hello, I am new to Jest and unit testing. I have a question about how to set the value of a text input using Vue Test Utils. Here is the code for my custom text input component: <input v-model="local_value" @keyup.enter="submitTo ...

javascript ondrag while self-pressing

Within this div, I have a series of p elements. My goal is to drag the selected element into the input field. Here's an example: var input = document.getElementById("test"); input.addEventListener('drop', function (event) { event.pr ...

Exploring the various functions of Android ListView

Currently, I am populating a custom ListView with data from a JSON request and would like to add an OnItemClickListener. Here is how I have implemented it: ListView lv = (ListView) findViewById(R.id.MessageList); lv.setOnItemClickListener(new android. ...

Is there a way to obtain precise latitude and longitude coordinates for my current GPS location?

When utilizing the GPS Tracker class, my aim is to obtain a precise current location. However, each time I attempt this, the location received differs with an accuracy of 20 meters. URL for the GPS Tracker Class: Code snippet: gpsTracker = new GPSTrac ...

What is the best way to retrieve data obtained through a node module and incorporate it into my HTML code within NodeWebkit?

I've been working on developing an app with NodeWebkit and utilizing the node-phantom-simple module for content scraping. While I have successfully scraped content from a website, I'm now wondering how I can access this data on the HTML side with ...

Using Three.js WebGL to create a custom circle with unique fill and border colors generated from a shader

Currently, I am utilizing Three.js alongside the WebGLRenderer. I am exploring ways or searching for an example on how to create circles using CircleGeometry and have the ability to manipulate their fill and border color through a vertex or fragment shad ...

What are the steps for releasing a collection of Vue.js components?

Currently, I am working on a project that involves a Vuex module and abstract components that users can extend. My goal is to clean up my codebase by separating this project into a well-tested module and publishing it on NPM. In order to achieve this, I ha ...

How to deactivate permissions on an Android device

I'm looking to develop an application that can disable certain permissions for other applications. For example, I want to prevent Facebook or Viber from accessing my location. I know of two apps that can help with this: This open-source application ...

Ajax updates to an element are not reflected until the for loop has completed

I am looking for a way to print a series of numbers sequentially using AJAX. Here is an example of what I want to achieve: (each new line represents an update of the previous line!) Output is: 1 12 123 1234 12345 123456 ... I ...

Tips for implementing the debounce function in CanJS DefineMap view-model method

I have been attempting to implement the lodash function _.debounce for a DefineMap view-model method in canjs. Even after trying to do this within the init method, I am having trouble with properly referencing 'this' inside the debounced function ...

The browser is not displaying the results from Mongodb, yet they are appearing in the console

I am currently using the following code for my router: let mongoose = require('mongoose'); // connecting with our model let ByProduct = require('../models/Byproduct') router.get('/',(req,res,next)=>{ ByProduct.find().th ...

Steps for making a dropdown input field:1. Start by defining a

Is there a way to design a dropdown input using CSS or any other method? Take a look at this image for reference: Any suggestions on how to achieve it? ...