0

I am trying to make an oracle connection. But this NullPointerException is really bugging the crap out of me.
Please any help would be appreciated.
Help me out with any mistakes and errors.

This is my code here:

public class MainActivity extends AppCompatActivity {

public Button btn_connection;
public TextView tv;
public OracleConnection oc;
public String Result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView);
    try {

        btn_connection = (Button) findViewById(R.id.connectionAttempt);
        btn_connection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Result = oc.ConnectionMade();
                tv.setText(Result);
            }
        });

    }
    catch(NullPointerException e) {
        tv.setText("Doll");

    }

}



}

Main Activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tahir.onlineshopping12.MainActivity">


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp" />

<Button
    android:id="@+id/connectionAttempt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Connect" />

OracleConnection class:

public class OracleConnection {



public String ConnectionMade(){
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection co = null;
        String Result = " ";

        try{
            co = DriverManager.getConnection(ipAddress,user,pass);
            DriverManager.registerDriver(new    oracle.jdbc.driver.OracleDriver());
            Result = "Successfully Connected";


        } catch (SQLException e) {

            Result = "Not Connected!!!!!!!!!" + e.toString();
        }


    return Result;

}

This is the error

java.lang.NullPointerException at com.example.tahir.onlineshopping12.MainActivity$1.onClick(MainActivity.java:28) at android.view.View.performClick(View.java:4461) at android.view.View$PerformClick.run(View.java:18514) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5129) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

3

You declare the class variable:

public OracleConnection oc;

but without instantiation, tried to use it.

Result = oc.ConnectionMade();

cipley
  • 1,091
  • 10
  • 15
0

check this line result have no value

Result = oc.ConnectionMade();
Sikander Bhutto
  • 226
  • 1
  • 11