-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
  • 13
  • 6
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AskNilesh Mar 23 '18 at 08:26
  • 1
    check your placesoutput layout. Does it contain any textview having R.id.hydtext1 as its id ? – Gautam Mar 23 '18 at 08:28
  • Could you paste the activity layout file as well . Also check if getIntent().getStringExtra("DatabaseOutput") is null. – Ashish Kumar Mar 23 '18 at 08:30
  • hi Gautam, really thank you, i got the issue resolved but it is returning nothing and my textview is showing blank – satish Mar 23 '18 at 08:48
  • i added activity layout also, could you please check now – satish Mar 23 '18 at 08:59

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
  • 424
  • 5
  • 20
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
  • i changed tgat id, but actually the issue is with SQL Query.. for confirmation i just ran the same query in Eclipse and it gave output but here it is showing Null.. here it is giving the output as Null – satish Mar 23 '18 at 10:07
  • From what you've provided, the ACTUAL issue is with the android code. [Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference] – HaroldHibari Mar 23 '18 at 10:10
  • hi harold now the issue is resolved and intent is happening but my textview is not set with any value as my sql query returning nothing but in eclipse when i execute that it return the value – satish Mar 23 '18 at 10:12
  • Have you tried putting a break point and debugging the app to see what is being returned by the SQL? – HaroldHibari Mar 23 '18 at 10:16
  • noo, i didnt tried.. but i tried to print in output it is printing output as null – satish Mar 23 '18 at 10:19
  • There could be many things causing your output to be null, your sql statement itself being one of them, so I'd advise you do this. Do you know how to debug with breakpoints? – HaroldHibari Mar 23 '18 at 10:29
  • Are you using Android Studio or Eclipse – HaroldHibari Mar 23 '18 at 11:36
  • Android Studio Harold – satish Mar 23 '18 at 19:26
  • Hii Harold,,It is throwing nullpointerException in my SQL Query – satish Mar 25 '18 at 07:20
  • java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.ResultSet java.sql.Statement.executeQuery(java.lang.String)' on a null object reference – satish Mar 25 '18 at 07:21
  • Could you then accept this answer and post a new question as this one is unrelated? I will be able to help you through that new question. – HaroldHibari Mar 25 '18 at 10:43
  • Can you please go this question https://stackoverflow.com/questions/49474625/getting-java-nullpointerexception-in-the-time-connecting-my-android-app-to-jdbc – satish Mar 25 '18 at 11:13
  • i posted a new question for that – satish Mar 25 '18 at 11:13
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

ישו אוהב אותך
  • 26,433
  • 11
  • 70
  • 92