views:

166

answers:

5

hi. i am facing one problem.i have a class named "ReportingService" and it should be singleton and is is extending "CommonService".

package MyApp.Services.ReportingService;

public class ReportingService extends CommonService {

    private static ReportingService instance = null;

    public static ReportingService getInstance() {
        if (instance == null) {
            instance = new ReportingService();
        }
        return instance;   

    }
}

and to access this class in other class i am using

package MyApp.Services.WebReportingService;
@WebMethod(operationName = "registerUDP")
    public boolean registerUDP(
            @WebParam(name = "Friendly Name") String friendlyName,
            @WebParam(name = "Username") String username,
            @WebParam(name = "Password") String password,
            @WebParam(name = "Communication Protocol") CommunicationProtocol communicationProtocol,
            @WebParam(name = "IP Address") String ipAddress,
            @WebParam(name = "Port") int port) {

        Consumer client = new Consumer(friendlyName, username, password, communicationProtocol, ipAddress, port);

ReportingService rs = ReportingService.
        return true;

    }

in "ReportingService rs = ReportingService." it is not showing me getInstance() method of ReportingService class. I also imported correct packages.

NOTE: both classes are in different packages.

here is the error when i compiled.

G:\mOhsan\JavaProjects\Care.AAD.SOA\Care.AAD.Java.Services.WebReportingService\src\java\Care\AAD\Java\Services\WebReportingService\WebReportingService.java:42: cannot access care.aad.Java.Common.DesignPattern.ObserverPattern.Observer class file for care.aad.Java.Common.DesignPattern.ObserverPattern.Observer not found Consumer client = new Consumer(friendlyName, username, password, communicationProtocol, ipAddress, port); G:\mOhsan\JavaProjects\Care.AAD.SOA\Care.AAD.Java.Services.WebReportingService\src\java\Care\AAD\Java\Services\WebReportingService\WebReportingService.java:44: cannot access care.aad.Java.Common.DesignPattern.ObserverPattern.Subject class file for care.aad.Java.Common.DesignPattern.ObserverPattern.Subject not found ReportingService rs = ReportingService.getInstance(); 2 errors G:\mOhsan\JavaProjects\Care.AAD.SOA\Care.AAD.Java.Services.WebReportingService\nbproject\build-impl.xml:422: The following error occurred while executing this line: G:\mOhsan\JavaProjects\Care.AAD.SOA\Care.AAD.Java.Services.WebReportingService\nbproject\build-impl.xml:225: Compile failed; see the compiler error output for details. BUILD FAILED (total time: 1 second)

A: 

You will have to import the ReportingService class at the top of your source file, like so:

import MyApp.Services.ReportingService.ReportingService;

Edit: The second code snippet is also lacking a class declaration. If this is the full source, it cannot compile.

Another edit based on compiler output: It would also help if you had the care.aad.Java.Common.DesignPattern.ObserverPattern.Observer class on the classpath that the Consumerclass seems to depend on.

Henning
A: 

Can't be sure, but I've seen this kind of problem happening when the imported classes are outdated with the code.

This can happen on different IDEs, there's no straightforward solution: Maybe cleaning all classes and compiling them again?

Rodrigo Gama
A: 

I would try typing getInstance() by hand, as others have suggested in their comments. That should work. The reason I believe your IDE will not show you the method after typing the dot (.) could be simply because of the following return true. It's reading

ReportingService rs = ReportingService.return true;

which it considers broken code and therefore will not provide code-completion.

Peter Perháč
+1  A: 

I think your package names are broken. You seem to have the name of the class at the end of the package - but the name of the class only appears when you import.

So you would have :

package myApp.Services;

public class ReportService extends CommonService {/* code goes here */}

but then you would include:

import myApp.Services.ReportService;

Same goes for the WebReportingService. The package statement should not include the class name

Edit: if you actually want ReportService in package myApp.Services.ReportService, then you need to import myApp.Services.ReportService.ReportService (or, alternatively, myApp.Services.ReportService.*, but this is not recommended unless you need many classes from the package)

laura
still unable to access. i tried complete class name in import statement on WebReportingService
Mohsan
try writing the whole package name in the code; something like: `myApp.Services.ReportService.ReportService rs = myApp.Services.ReportService.ReportService.getInstance()` . Also check the `Consumer` class, there seems to be an issue with that as well (it doesn't find the `Observer` class, possibly another import blunder)
laura
+3  A: 

Change as follows:

package MyApp.Services.ReportingService;  // FIXME - Make package names lowercase!!
                                          // FIXME - Loopy package name

public class ReportingService extends CommonService {

    private static ReportingService instance = null;

    private ReportingService() { }

    public static synchronized ReportingService getInstance() {
        if (instance == null) {
            instance = new ReportingService();
        }
        return instance;   

    }
}

and

import MyApp.Services.ReportingService.ReportingService; 

package MyApp.Services.WebReportingService ; 
                        // FIXME - Make package names lowercase!!
                        // FIXME - Loopy package names.

public class WebReportingService {

    @WebMethod(operationName = "registerUDP")
    public boolean registerUDP(
            @WebParam(name = "Friendly Name") String friendlyName,
            @WebParam(name = "Username") String username,
            @WebParam(name = "Password") String password,
            @WebParam(name = "Communication Protocol") CommunicationProtocol communicationProtocol,
            @WebParam(name = "IP Address") String ipAddress,
            @WebParam(name = "Port") int port) {

        Consumer client = new Consumer(friendlyName, username, password,
                                       communicationProtocol, ipAddress, port);

        ReportingService rs = ReportingService.getInstance();
        return true;
    }
}

Note: depending on which packages the Consumer and CommunicationProtocol classes are defined in, you may need to import them; e.g. add these lines before the package line.

import some.package.Consumer;
import some.other.package.CommunicationProtocol;

Note 2: your current choice of package names has some seriously style issues.

  • package names should be all lowercase

  • the prefix of your package names should be the (reversed) DNS-style identifier for your company, organization, whatever; e.g. in.com.yourcompany.... Note, there are very good reasons form this convention!!

  • MyApp / myapp is content free and shouldn't be used

  • services.reportingservice is redundant / verbose; e.g. use services.reporting instead

  • using the same name as a class and package name is redundant / verbose

  • unless there are lots of classes in the reporting and webreporting packages, they probably should be folded into one package. Lots of packages with 1 or 2 classes in them is not helpful.

Stephen C
these two classes are in different packages
Mohsan
still not compiling :(
Mohsan
So do you really have a classes called MyApp.Services.WebReportingService.WebReportingService and MyApp.Services.ReportingService.ReportingService ??? That seems a bit loopy to me.
Stephen C
yes there are classes. look "MyApp.Services.ReportingService" is package name and "ReportingService" is class in this package. so that when i import this package i am writing "MyApp.Services.ReportingService.ReportingService"
Mohsan
Changed per your comments. Try again.
Stephen C
+1 for the `synchronized` getInstance()! Also, the singleton needs a declared `private` constructor (just in case that's the whole code).
laura
problem solved. there was some libraries issues.
Mohsan