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 8223337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:39:34+00:00 2026-06-07T14:39:34+00:00

I created a service to get data from the /proc/meminfo and the service class

  • 0

I created a service to get data from the /proc/meminfo and the service class is :

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MonitorService extends Service{

Vector<String> memFree, buffers, cached, active, inactive, swapTotal,dirty;
int TOTAL_INTERVALS = 440;
private BufferedReader readStream;
private String x;
int memTotal;

private Runnable readRunnable = new Runnable() {
    public void run() {
        read(); // We call here read() because to draw the graphic we need
                // at less 2 read values.
        while (readThread == Thread.currentThread()) {
            try {
                read();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
};

private Thread readThread = new Thread(readRunnable, "readThread");

class MyServiceDataBinder extends Binder {
    MonitorService getService() {
        return MonitorService.this;
    }
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    memFree = new Vector<String>(this.TOTAL_INTERVALS);
    buffers = new Vector<String>(this.TOTAL_INTERVALS);
    cached = new Vector<String>(this.TOTAL_INTERVALS);
    active = new Vector<String>(this.TOTAL_INTERVALS);
    inactive = new Vector<String>(this.TOTAL_INTERVALS);
    swapTotal = new Vector<String>(this.TOTAL_INTERVALS);
    dirty = new Vector<String>(this.TOTAL_INTERVALS);

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return new MyServiceDataBinder();
}

private void read() {
    try {
        readStream = new BufferedReader(new FileReader("/proc/meminfo"));
        x = readStream.readLine();
        while (x != null) {

            /*
             * When the limit TOTAL_INTERVALS is surpassed by some vector we
             * have to remove all the surpassed elements because, if not,
             * the capacity of the vector will be increase x2.
             */
            while (memFree.size() >= this.TOTAL_INTERVALS)
                memFree.remove(memFree.size() - 1);
            while (buffers.size() >= this.TOTAL_INTERVALS)
                buffers.remove(buffers.size() - 1);
            while (cached.size() >= this.TOTAL_INTERVALS)
                cached.remove(cached.size() - 1);
            while (active.size() >= this.TOTAL_INTERVALS)
                active.remove(active.size() - 1);
            while (inactive.size() >= this.TOTAL_INTERVALS)
                inactive.remove(inactive.size() - 1);
            while (swapTotal.size() >= this.TOTAL_INTERVALS)
                swapTotal.remove(swapTotal.size() - 1);
            while (dirty.size() >= this.TOTAL_INTERVALS)
                dirty.remove(dirty.size() - 1);

            // We read the memory values. The percents are calculated in the
            // AnotherMonitor class.
            if (x.startsWith("MemTotal:"))
                memTotal = Integer.parseInt(x.split("[ ]+", 3)[1]);
            if (x.startsWith("MemFree:"))
                memFree.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("Buffers:"))
                buffers.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("Cached:"))
                cached.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("Active:"))
                active.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("Inactive:"))
                inactive.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("SwapTotal:"))
                swapTotal.add(0, x.split("[ ]+", 3)[1]);
            if (x.startsWith("Dirty:"))
                dirty.add(0, x.split("[ ]+", 3)[1]);
            x = readStream.readLine();
        }

        readStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

The class where I call the created service :

 import java.text.DecimalFormat;
 import java.util.Vector;

 import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.widget.TextView;

 import com.sliit.droidman.R;

 public class Processor extends Activity{

private TextView ram1, ram2, ram3, ram4, ram5, ram6, ram7;
private MonitorService MyMonitor;
private DecimalFormat myFormat = new DecimalFormat("##,###,##0");
private Handler myHandler;

private Runnable drawRunnable = new Runnable() {
    public void run() {
        setTextLabel(ram1,MyMonitor.memFree);
        setTextLabel(ram2,MyMonitor.buffers);
        setTextLabel(ram3,MyMonitor.cached);
        setTextLabel(ram4,MyMonitor.active);
        setTextLabel(ram5,MyMonitor.inactive);
        setTextLabel(ram6,MyMonitor.swapTotal);
        setTextLabel(ram7,MyMonitor.dirty);

        myHandler.postDelayed(this, 1000);
    }
};

private ServiceConnection MyService = new ServiceConnection() {

    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        MyMonitor = null;
    }

    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        MyMonitor = ((MonitorService.MyServiceDataBinder)service).getService();
        ram1.setText(myFormat.format(MyMonitor.memTotal) + " kB");
        myHandler.post(drawRunnable); <--- A null pointer Exceprtion occurs
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    startService(new Intent(this, MonitorService.class));
    setContentView(R.layout.resources_processor);

    ram1 = (TextView) findViewById(R.id.Appres_ProcessorInfo1);
    ram2 = (TextView) findViewById(R.id.Appres_ProcessorInfo2);
    ram3 = (TextView) findViewById(R.id.Appres_ProcessorInfo3);
    ram4 = (TextView) findViewById(R.id.Appres_ProcessorInfo4);
    ram5 = (TextView) findViewById(R.id.Appres_ProcessorInfo5);
    ram6 = (TextView) findViewById(R.id.Appres_ProcessorInfo6);
    ram7 = (TextView) findViewById(R.id.Appres_ProcessorInfo7);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    bindService(new Intent(this, MonitorService.class), MyService, 0);
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    unbindService(MyService);
}

private void setTextLabel(TextView textView, Vector<String> y) {
        if (!y.isEmpty()) {
            textView.setText(myFormat.format(Integer.parseInt(y.firstElement())) + " kB");
            //textViewP.setText(myFormatPercent.format(Integer.parseInt(y.firstElement())* 100 / (float) myAnReaderService.memTotal) + "%");
        } else
            textView.setText("Value");
}

 }

The log cat out put :

07-13 10:20:08.109: E/AndroidRuntime(745): FATAL EXCEPTION: main
07-13 10:20:08.109: E/AndroidRuntime(745): java.lang.NullPointerException
07-13 10:20:08.109: E/AndroidRuntime(745):  at com.sliit.droidman.systemresources.Processor$2.onServiceConnected(Processor.java:49)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1097)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1114)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.os.Handler.handleCallback(Handler.java:615)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.os.Looper.loop(Looper.java:137)
07-13 10:20:08.109: E/AndroidRuntime(745):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-13 10:20:08.109: E/AndroidRuntime(745):  at java.lang.reflect.Method.invokeNative(Native Method)
07-13 10:20:08.109: E/AndroidRuntime(745):  at java.lang.reflect.Method.invoke(Method.java:511)
07-13 10:20:08.109: E/AndroidRuntime(745):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-13 10:20:08.109: E/AndroidRuntime(745):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-13 10:20:08.109: E/AndroidRuntime(745):  at dalvik.system.NativeStart.main(Native Method)
07-13 10:20:15.860: E/Trace(764): error opening trace file: No such file or directory (2)

I get a null pointer exception from the code that I have pointed out in the code. Please help me out to figure out the error that i DO in this section.

thank you.

  • 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-07T14:39:36+00:00Added an answer on June 7, 2026 at 2:39 pm

    This line:

     private Thread readThread = new Thread(readRunnable, "readThread");
    

    You create this thread in service, but never start/call it. That is the reason your vectors are empty. Try starting it in onBind().

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

Sidebar

Related Questions

I'm using C# to read data from a Java-webservice. I've created a Service reference
I want to import data from java web application to sugarCRM. I created client
Suppose I get data from a service (that I can't control) as: public class
I've created a WCF service which I intend to use when sending data from
I have created a Service in android which can send data to the activity
I have created a component to fetch data from a web service. The web
I have created a C# Windows service but it fails to start. I get
When android unbind a service I created (service.MyService), I see the following DeadObjectException. Can
I created a service (service B) from Activity (Activity A). And from service B,
I have created a service class for my network connection so that my app

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.