0

I have a chunk of code which is called at my application startup to find the ProgramData folder of the users PC and to create the application.log file for my app.

Problem is that in one of the users PC having windows 7, its throwing nullpoint error in the Java tracing pop up. I am not sure how to recreate or debug this issue, as this is the step to create the log file and put in ProgramData folder.

Any help is really appreciated!

    final int HKEY_LOCAL_MACHINE = 0x80000002;
    final int KEY_QUERY_VALUE = 1;
    final Preferences systemRoot = Preferences.systemRoot();
    final Preferences userRoot = Preferences.userRoot();
    final Class clz = userRoot.getClass();
    String appData = null;
    try {
        Method openKey = clz.getDeclaredMethod("WindowsRegOpenKey",
                new Class[] { int.class, byte[].class, int.class });
        openKey.setAccessible(true);
        Method closeKey = clz.getDeclaredMethod("WindowsRegCloseKey",
                new Class[] { int.class });
        closeKey.setAccessible(true);
        Method queryKey = clz.getDeclaredMethod("WindowsRegQueryValueEx",
                new Class[] { int.class, byte[].class });
        queryKey.setAccessible(true); 
        int[] result = (int[]) openKey
                .invoke(
                        systemRoot,
                        new Object[] {
                                new Integer(HKEY_LOCAL_MACHINE),
                                toCstr("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"),
                                new Integer(KEY_QUERY_VALUE) });
    int handle = result[0];
    if(handle!=0){
        byte[] bytes =(byte[]) queryKey.invoke(
                systemRoot,
                new Object[] {
                        handle,
                        toCstr("Common AppData")});
        appData = new String(bytes,"US-ASCII");
        closeKey.invoke(systemRoot,new Object[]{handle});
    }
    } catch (Exception e1) {
        //log.error(e1.getStackTrace());
    }
timiTao
  • 1,417
  • 3
  • 20
  • 34
  • More information what happened would have been in the exception stack trace (which you decided to ignore in your catch clause) – Thomas Fritsch Mar 05 '18 at 07:24
  • If you fail in the process of finding the appropriate location for the log file, you could display a file-chooser, and ask the user to select the location to save the log. (Or pick a default, like ${HOMEDRIVE}${HOMEPATH} or ${APPDATA} or C:\temp\) --- that will at least let you log the stack trace to figure out what's going on. – lockcmpxchg8b Mar 05 '18 at 07:30
  • Thomas i commented just for this post even if i just put e1.getStackTrace(), where it will show the details? And even if i keep the stackTrace it will just show the null[pointer exception. Currently its not showing which line is having nullpointer exception. – Rajesh Singh Mar 05 '18 at 07:49
  • What library do you use for calling WindowsAPI methods/registry access? – Robert Mar 05 '18 at 08:24
  • There is no such thing as a '`nullpoint` error'. – user207421 Mar 05 '18 at 08:53

0 Answers0