I get this error
java.lang.NullPointerException at android.content.ContextWrapper.getPackageManager
when I try to get a list of all installed applications on the device.
I have a server that starts when my application starts, and the client binds the server and requests a list of installed applications. The server then requests getPackageManager() and receives all installed applications. But getPackageManager throws a NullPointerException .
The server is written in Java and runs from my Android application.
Can someone please tell me what is missing and why am I getting this error?
Please find the code below.
public class ApplicationRecognition extends Activity { // android.os.Debug.waitForDebugger(); Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.buttonStart); buttonStop = (Button) findViewById(R.id.buttonStop); final SecurityModuleServer server = new SecurityModuleServer(5902); server.start(); Toast.makeText(this, "Application Server is started", Toast.LENGTH_SHORT).show(); buttonStart.setOnClickListener(new OnClickListener(){ public void onClick(View v) { startService(new Intent(getBaseContext(), AppReconService.class)); }}); buttonStop.setOnClickListener(new OnClickListener(){ public void onClick(View v) { stopService(new Intent(getBaseContext(), AppReconService.class)); }}); } public String[] getInstalledApplications() { String[] appname =new String[10]; Intent mainIntent = new Intent(Intent.ACTION_MAIN,null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); PackageManager manager = getPackageManager(); List<ResolveInfo> pkgAppsList = manager.queryIntentActivities(mainIntent, 0); appname = new String[pkgAppsList.size()]; for(int i=0;i<pkgAppsList.size();i++) { appname[i]=pkgAppsList.get(i).activityInfo.packageName; } return appname; }
}
server-side code Public class SecurityModuleServer implements Observer, Runnable {private int numberOfConnectedClient; private thread serverThread; private ServerSocket serverSocket; private volatile boolean isServerRunning;
public SecurityModuleServer(final int port) { numberOfConnectedClient = 0; try { serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } public void run() { System.out.println("SecurityModuleServer>> server thread started."); //$NON-NLS-1$ while(isServerRunning) { numberOfConnectedClient++; try { SecurityModuleClientThread client = new SecurityModuleClientThread(serverSocket.accept(), numberOfConnectedClient); client.addObserver(this); client.start(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("SecurityModuleServer>> server thread stopped."); //$NON-NLS-1$ } synchronized public void start() { serverThread = new Thread(this); isServerRunning = true; serverThread.start(); } synchronized public void stop() { isServerRunning = false; } public boolean isRunning() { return isServerRunning; } public static void main(String[] args) { SecurityModuleServer server = new SecurityModuleServer(5903); server.start(); } public void update(Observable o, Object arg) { numberOfConnectedClient--; }
}
client side code
public SecurityModuleClientThread(Socket socket, int numberOfClient) { clientSocket = socket; numberOfConnectedClient = numberOfClient; try { printOut = new PrintStream(clientSocket.getOutputStream()); readerIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); clientThread = new Thread(this); } catch (IOException e) { e.printStackTrace(); } } public void run() { Looper.prepare(); String input = ""; //$NON-NLS-1$ System.out.println("SecurityModuleClientThread>> thread started for client."); //$NON-NLS-1$ if (numberOfConnectedClient <= MAX_ALLOWED_CLIENTS) { printOut.println(CMD+Answer_Open_Connection+SEPARATOR+M002); while(isClientRunning) { try { input = readerIn.readLine(); System.out.println("Message received>> "+input); //$NON-NLS-1$ parseInputMessage(input); } catch (IOException e) { e.printStackTrace(); } } } else { printOut.println(CMD+Error+SEPARATOR+M003); stop(); } System.out.println("SecurityModuleClientThread>> thread stopped for client."); //$NON-NLS-1$ } private void parseInputMessage(final String input) { if (input.equalsIgnoreCase(CMD+Request_Close_Connection)) { printOut.println(CMD+Answer_Close_Connection+SEPARATOR+M007); stop(); } else { String messages[] = input.split(SEPARATOR); // Parse the command switch(parseCommand(messages[0])) { case Request_Start_Application: if(parseApplicationName(input) != null) { if(startAndroidApplication(parseApplicationName(input))) { // TODO printOut.println(CMD+Answer_Start_Application); startAndroidApplication(parseApplicationName(input)); } else { printOut.println(CMD+Error+SEPARATOR+M004); } } else { printOut.println(CMD+Error+SEPARATOR+M004); } break; case Request_Stop_Application: if(parseApplicationName(input) != null) { if (stopAndroidApplication(parseApplicationName(input))) { // TODO printOut.println(CMD+Answer_Stop_Application); } else { printOut.println(CMD+Error+SEPARATOR+M004); } } else { printOut.println(CMD+Error+SEPARATOR+M004); } break; case Request_Application_Installed: String[] appnames = new String[provideInstalledApplication().length]; appnames = provideInstalledApplication(); for(int i=0;i<appnames.length;i++) { printOut.println(appnames[i]); } break; case Request_Application_Running: //TODO break; default: printOut.println(CMD+Error+SEPARATOR+M008); break; } } } private int parseCommand(String cmd) { if (cmd.length() == 6) { return Integer.parseInt(cmd.substring(3, 6)); } else { return 0; } } private String parseApplicationName(String message) { if (message.length() > 6) { // TODO return message.substring(6, message.length()); } else { return null; } } public synchronized void start() { isClientRunning = true; clientThread = new Thread(this); clientThread.start(); } public synchronized void stop() { printOut.close(); try { readerIn.close(); clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } finally { isClientRunning = false; setChanged(); notifyObservers(); } } public boolean isRunning() { return isClientRunning; } public String[] provideInstalledApplication() { String[] appnames = null; ApplicationRecognition apprecon = new ApplicationRecognition(); appnames=new String[apprecon.getInstalledApplications().length]; appnames = apprecon.getInstalledApplications(); return appnames; } public String[] provideRunningApplication() { // TODO Auto-generated method stub return null; } public boolean startAndroidApplication(String applicationName) { // TODO Auto-generated method stub ApplicationRecognition apprecon = new ApplicationRecognition(); apprecon.startApplication(applicationName); return false; } public boolean stopAndroidApplication(String applicationName) { // TODO Auto-generated method stub return false; }
}
The main problem that causes me problems is that the ApplicationRecognition class in the getInstalledApplications() method in getPackageManager is null. This method is called from the SecurityModuleClientThread class on the client side of the provideInstalledApplication .
Can someone please tell me where I will be wrong.