2

Im trying to do a simple application in android with a new class for learn how to use it. The main activity have :

package com.josejoaquin.testhttp;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MyActivity extends ActionBarActivity {



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

    Button Boton = (Button)findViewById(R.id.button);
    TextView Texto = (TextView)findViewById(R.id.textView);

    Boton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clientehttp clienteweb = null;
            String total;
            total = clienteweb.getWeb();
            Texto.setText(total);

        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

And the other file is named clientehtt.java and have this code:

package com.josejoaquin.testhttp;


public class clientehttp {

   public String getWeb (){

    String texto ="Hola Mundo";
    return texto;


}
}

THe manifiest file have:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.josejoaquin.testhttp" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

But i get and error when i do a click the applition close, what im doing wrong? Can anyone help me to learn more about this?

Best Regards.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Roke
  • 329
  • 1
  • 3
  • 16

1 Answers1

0

I assume you get a NullPointerException.

The problem is inside your onClick() method:

        clientehttp clienteweb = null;
        String total;
        total = clienteweb.getWeb();

You are setting the value of clienteweb to null but you are then attempting to use its method clienteweb.getWeb() and you cannot do that.

Change your code to:

        clientehttp clienteweb = new clientehttp();
        String total;
        total = clienteweb.getWeb();

and everything should work. If you are wondering what exactly was wrong, think about null meaning empty or nothing. If your clienteweb is empty, it pretty much doesn't exist yet and thus doesn't have any methods such as getWeb() for you to call.

So the statement:

clientehttp clienteweb = null;

Loosely means "I declare a variable of type clientehttp and initialize it to nothing"

Once you instantiate it (with the new expression), then it becomes "filled" and you can use its methods. For more info you should read up on what null is and how java declares/instantiates/initializes objects.

Also, you should probably use Java's standard naming conventions to make your code cleaner. One specific convention you should add is to name all your classes with a starting capital letter (If you are using Eclipse it probably already warns you to do that)

So rename the class clientehttp to Clientehttp so its clearer that it is a class. The easiest way to do that is to right-click on the clientehttp.java and do Refractor/Rename and the change will be applied everywhere.

Community
  • 1
  • 1
nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thank you, works like a charm but i didnt understand why to put new clientehttp() if im creating a new variable of this type. – Roke Sep 15 '14 at 20:21
  • @user1532116 well that's exactly why. You use the `new` expression to create a new object. You are welcome :) Read the links I posted in my answer for more info. – nem035 Sep 15 '14 at 20:22