Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:21:05+00:00 2026-06-01T01:21:05+00:00

I’m trying to implement ZXing barcode scanner into my program. After getting the scanned

  • 0

I’m trying to implement ZXing barcode scanner into my program. After getting the scanned result, I wanted to parse the result to a method named getData() which belong to another Java class. No syntax error on IDE, but mysql.getData(contents) won’t call the method no matter what. Please advise.

If I put this code under onCreate, the whole program force closed:

package com.posQR.ip;

import com.posQR.ip.MySQL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PosQRActivity extends Activity{
MySQL mysql;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    
    mysql.getData("productID");
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(scanListener);

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    TextView textView = (TextView)findViewById(R.id.textView1);
    if (requestCode == 0) {
    if (resultCode == RESULT_OK) {
    try {
        String contents = intent.getStringExtra("SCAN_RESULT");
        Toast.makeText(getApplicationContext(), contents + "from main", Toast.LENGTH_LONG).show();
        mysql.getData(contents);
        String string = Double.toString(mysql.getPrice());
        textView.setText(string);
    }
    catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Please scan on the product's QR Code.", Toast.LENGTH_LONG).show();
    }
    } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    }

    }
    }

    private OnClickListener scanListener = new OnClickListener() {
    public void onClick(View v) {
    try{
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    startActivityForResult(intent, 0);
    }
    catch (Exception e){
    Toast.makeText(getApplicationContext(), "error opening scanner.", Toast.LENGTH_SHORT).show();
    }
    }
    };
}

.

package com.posQR.ip;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.net.ParseException;
import android.util.Log;
import android.widget.Toast;

public class MySQL{
double Price;
private Context localContext;

public void getData(String productID) {
InputStream is = null;
String result = "";

Toast.makeText(localContext.getApplicationContext(), productID, Toast.LENGTH_LONG).show();

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",productID));
//http post
try{
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://dl.dropbox.com/u/11233767/mysqlRequest.php");
 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 HttpResponse response = httpclient.execute(httppost);
 HttpEntity entity = response.getEntity();
 is = entity.getContent();
 }catch(Exception e){
     Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(localContext.getApplicationContext(), "Error converting result.",    Toast.LENGTH_LONG).show();
}

//paring data

try{
  JSONArray jArray = new JSONArray(result);
  JSONObject json_data=null;
         json_data = jArray.getJSONObject(0);
         Price=json_data.getDouble("price");

  }
  catch(JSONException e1){
      Toast.makeText(localContext.getApplicationContext(), "No City Found" ,Toast.LENGTH_LONG).show();
  } catch (ParseException e1) {
        e1.printStackTrace();
}
}

public double getPrice(){
return Price;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:21:06+00:00Added an answer on June 1, 2026 at 1:21 am

    There are many problems here, such as:

    1. the mysql variable is null when you use it in onCreate, you must declare it.

    2. The localContext object in MySql is never initialized, which will cause another NullPointerException when you call getData(). You should create a constructor that accepts a Context object for the MySql class, and use that constructor when you’re fixing the first problem. Alternatively you could pass a Context object to getData()

    3. the getData() method accesses the network in the main/UI thread, which will cause another Exception. Call this method in a different thread or spin off a new thread within the method itself. You may want to use an AsyncTask

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.