-1

////**This is My Fragment Code****////

here i in onclick method based on the option selected in the list specific sql query is executed and gives the output and that output will be display in another activity

 public class TouristPlace extends Fragment implements AdapterView.OnItemClickListener {
    public ListView list;
    public ArrayAdapter<String> arrayAdapter;
    public ResultSet result;
    public Statement statement;
    public String Desc;
    public String output;
    public TouristPlace touristPlace;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tourists, container, false);
            list = (ListView)rootView.findViewById(R.id.places);
            touristPlace=new TouristPlace();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Welcome_to_hyd", "root", "test");
                statement = con.createStatement();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            final String[] placelist = getResources().getStringArray(R.array.menu);
            arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, placelist);
            list.setAdapter(arrayAdapter);
            list.setOnItemClickListener(this);
            return rootView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
                case 0:
                    output=touristPlace.result("Golconda");
                    Intent i = new Intent(getActivity(), ListNavigation.class);
                    i.putExtra("DatabaseOutput", output);
                    startActivity(i);
            }
        }

        public String result(String city){
            String Fort = "Select Description from testHyd where TouristPlace="+city;
            try {
                result = statement.executeQuery(Fort);
                while (result.next()) {
                     Desc=result.getString(1);
                    System.out.println(Desc);
                }
            }catch (Exception e){
                e.printStackTrace();;
            }
            return Desc;
        }
    }

////Activity to get Text from Fragment////

public class ListNavigation extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.placesoutput);
        TextView tv=findViewById(R.id.hydtext1);
        tv.setText(getIntent().getStringExtra("DatabaseOutput"));
    }
}

////LogCat////

it is giving null pointer exception for setting the text for textview

Process: com.cityzers.welcometohyderabad, PID: 2973
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cityzers.welcometohyderabad/com.cityzers.welcometohyderabad.ListNavigation}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                 at com.cityzers.welcometohyderabad.ListNavigation.onCreate(ListNavigation.java:23)
                                                 at android.app.Activity.performCreate(Activity.java:6679)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:154) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

////Activity layout////

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="357dp"
        android:layout_height="150dp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="13dp"
        android:src="@drawable/golconda"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16dp"
        android:textStyle="bold"
        android:layout_below="@id/imageView2"
        android:layout_marginTop="15dp"
        android:id="@+id/textview"/>
</RelativeLayout>
Satish Rongala
  • 209
  • 6
  • 19

3 Answers3

0

It's an error of id mismatch, in your layout you have given id android:id="@+id/textview" and in your activity you are doing TextView tv=findViewById(R.id.hydtext1); Both ids are different. There is no textView with id hydtext1 in your layout XML.

Megha Maniar
  • 444
  • 5
  • 22
0

In your onCreate method, you're trying to find the id "hydtext1" which does not exist in your XML layout.

TextView tv=findViewById(R.id.hydtext1);

Hence the setText function is flagging up an NPE. Change it to

TextView tv=findViewById(R.id.textview);
HaroldHibari
  • 422
  • 3
  • 12
0

This is because in your placesoutput layout you don't have any TextView with hydtext1 id. Your layout should be include the TextView something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

     ...

    <TextView
        android:id="@+id/hydtext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16dp"
        android:textStyle="bold"
        android:layout_below="@id/imageView2"
        android:layout_marginTop="15dp"/>
</RelativeLayout>

This is a common problem which arise because of no usage of naming convention. To prevent the same problem happened in the future, you need to use a proper naming convention.

Here is the convention:

  1. Activity name
    You should naming your activity with Activity as its postfix. For example, use ListNavigationActivity instead of ListNavigation.

  2. Layout name
    use activity name for a layout but in backward order. For example, use activity_list_navigation.xml instead placesoutput.xml.

  3. View name .
    You should naming your view inside a layout with the layout name and adding the abbreviation of the view as the postfix. For example, if your TextView inside of activity_list_navigation.xml you need to name it as list_navigation_name_tv

Please check Ribot Project Guidelines or Joielechong Projet Guidelines

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96