java.lang.NullPointerException at android.content.ContextWrapper.getPackageManager
<p>i am getting this error </p>

<pre><code>java.lang.NullPointerException at android.content.ContextWrapper.getPackageManager
</code></pre>

<p>when am trying to get list of all installed applications on the device.</p>

<p>Well let me explain what my program is doing.
I have a server that starts when my application is started, and the client pings the server and asks to get a list of installed applications. The Server then asks the getPackageManager() and gets all the installed applications.
But the getPackageManager returns the nullpointerexception.</p>

<p>The Server is written in a java environment and is started from my android application.</p>

<p>Could someone please tell me what am missing and why i am getting this error?</p>

<p>thanks alot!!!Please find the code below</p>

<pre><code>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&lt;ResolveInfo&gt; pkgAppsList = manager.queryIntentActivities(mainIntent, 0);
                 appname = new String[pkgAppsList.size()];
                 for(int i=0;i&lt;pkgAppsList.size();i++)
                 {
                     appname[i]=pkgAppsList.get(i).activityInfo.packageName;
                 }




            return appname;
        }
</code></pre>

<p>}</p>

<p>the server side code 
public class SecurityModuleServer implements    Observer,Runnable
{
    private int numberOfConnectedClient;
    private Thread serverThread;
    private ServerSocket serverSocket;
    private volatile boolean isServerRunning;</p>

<pre><code>public SecurityModuleServer(final int port)
{
    numberOfConnectedClient = 0;

            try 
            {
                serverSocket = new ServerSocket(port);

            } catch (IOException e) {

                e.printStackTrace();
            }

}


public void run()
{

    System.out.println("SecurityModuleServer&gt;&gt; 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&gt;&gt; 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--;
}
</code></pre>

<p>}</p>

<p>the client side code </p>

<pre><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&gt;&gt; thread started for client."); //$NON-NLS-1$

    if (numberOfConnectedClient &lt;= MAX_ALLOWED_CLIENTS)
    {   
        printOut.println(CMD+Answer_Open_Connection+SEPARATOR+M002);            
        while(isClientRunning)
        {
                try
                {
                     input = readerIn.readLine();
                    System.out.println("Message received&gt;&gt; "+input); //$NON-NLS-1$

                    parseInputMessage(input);


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

    }
    else
    {   
        printOut.println(CMD+Error+SEPARATOR+M003);
        stop();

    }

    System.out.println("SecurityModuleClientThread&gt;&gt; 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&lt;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() &gt; 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;
}
</code></pre>

<p>}</p>

<p>the main part that is giving me trouble is in ApplicationRecognition class under method getInstalledApplications()
in getPackageManager is null.
This method is called from the client side SecurityModuleClientThread class, provideInstalledApplication method.</p>

<p>Can someone please tell me where am i going wrong.</p>
