Simple App Won't Compile in Eclipse (with plugin)? - java

my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}

The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}

public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}

try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());

Related

Eclipse is running my previous .java file not the current one I want to Run

I created this ArrayLocation.java yesterday
public class ArrayLocation {
private double coords[];
public ArrayLocation (double[] coords) {
this.coords = coords;
}
public static void main (String[] args) {
double[] coords = {5.0, 0.0};
ArrayLocation accra = new ArrayLocation (coords);
coords[0] = 32.9;
coords[1] = -117.2;
System.out.println(accra.coords[0]);
}
} ---This ran well and gave me an output 32.9
Today I created a new project and added this MyDisplay.java to it
package guimodule;
import processing.core.PApplet;
public class MyDisplay extends PApplet {
public void setup(){
size(400, 600);
}
public void draw(){
}
}
But when I run MyDisplay.java eclipse is still running the old ArrayLocation.java and returning 32.9. I expected it would open a blank applet.
Please help. I am pretty new to eclipse
How should I tell eclipse to run MyDisplay.java and not ArrayLocation.java ?
Goto Projects -> Clean
Open your java file in the editor, right click Run as -> Java Application
There is no main method in your MyDisplay.java.
Check your BuildAnt
it will surely help you.
Cheers.
You may add main method to MyDisplay.java class itself as follows:
package guimodule;
import processing.core.PApplet;
public class MyDisplay extends PApplet {
public void setup(){
size(400, 600);
}
public void draw(){
}
public static void main(String args[]){
PApplet.main(new String[] {"guimodule.MyDisplay"});
}
}
OR you can also initiate your MyDisplay.java from other class having main function:
InitClass.java
public class InitClass{
public static void main(String args[]){
final MyDisplay disp = new MyDisplay();
disp.init();
}

Java Equivalent of C#'s Action Type in terms of efficiency. Java 8 or Pre-Java 8?

Does Java have anything similar to C#'s Action type? Is Java 8 or Pre-Java 8 the way to go? Why or why not? I'm trying to avoid going down any rabbit holes. Please help me understand my options...
Statement:
Driver.NoWait(() => links = rowFindElements(ByLinkText(title)));
Methods:
public static void NoWait(Action action)
{
TurnOffWait();
action();
TurnOnWait();
}
public static void TurnOnWait()
{
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
}
public static void TurnOffWait()
{
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));
}
UPDATE
Thanks to #Nick Y and a programmer at the office (who told me the history of Java pragmatics vs Java traditionalists). This is the outcome of my findings:
Feature Menu Class 1st Way Post Java 8
public class FeatureMenu
{
static WebElement sideTab;
public static void Expand()
{
try
{
Iframe.Default.SwitchTo();
Driver.NoWait(() -> sideTab = Driver.Instance.findElement(By.cssSelector("div.wijmo-wijsplitter-v-panel1-collapsed")));
sideTab.click();
Log.Info("Feature Menu Expanded.");
}
catch(Exception e)
{
Log.Error("[EXCEPTION CAUGHT] : FeatureMenu.Expand()");
throw(e);
}
}
}
Feature Menu 2nd Way Pre Java 8
public class FeatureMenu
{
static WebElement sideTab;
public static void Expand()
{
try
{
Iframe.Default.SwitchTo();
Driver.NoWait( new Driver.Action(){ public void apply(){
sideTab = Driver.Instance.findElement(By.cssSelector("div.wijmo-wijsplitter-v-panel1-collapsed"));
}
});
sideTab.click();
Log.Info("Feature Menu Expanded.");
}
catch(Exception e)
{
Log.Error("[EXCEPTION CAUGHT] : FeatureMenu.Expand()");
throw(e);
}
}
}
Driver Class that can be used with either approach
public class Driver
{
public static WebDriver Instance;
public static String BaseAddress(String baseAddress)
{
return baseAddress;
}
public static void Initialize(String driverType)
{
Instance = new FirefoxDriver();
Instance.manage().window().maximize();
TurnOnWait();
}
#FunctionalInterface
public interface Action {
void apply();
}
public static void NoWait(Action action)
{
TurnOffWait();
action.apply();
TurnOnWait();
}
public static void TurnOffWait()
{
Instance.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
}
public static void TurnOnWait()
{
Instance.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
}
If you want to get closer to the most recent C# version you would want to use Java 8 (not pre-Java 8)
Java 8 has lambdas and functional interfaces which can get you very close to how things are done in C#. Google "functional interface java". There is a lot good information out there.
In the context of your specific question think about functional interfaces in java as delegates in C#.
public delegate void Action()
can be mimicked in java 8 as
#FunctionalInterface
public interface Action() {
void apply();
}
With this in mind, here is the simple usage of Action interface and lambda
public class MainWithAction {
public static void main(String[] args) {
noWait(() -> doSomething());
}
public static void noWait(Action action) {
turnOffWait();
action.apply();
turnOnWait();
}
public static void doSomething() { /* do something */ }
public static void turnOnWait() { /* skipped */ }
public static void turnOffWait() { /* skipped */ }
}
It is not a requirement to use #FunctionalInterface annotation but it helps compiler to generate error messages in certain cases.
apply() method name can be changed to anything else it is more of a convention thing.
Java 8 has a few predefined functional interfaces in package java.util.function however it appears that there is nothing that returns void and takes no parameters so you would need to have your own. Read more here:
https://softwareengineering.stackexchange.com/questions/276859/what-is-the-name-of-a-function-that-takes-no-argument-and-returns-nothing
You may want to consider having NoWaitAction interface which can be a more appropriate name for your scenario instead of a generic Action interface. It's up to you.
Having said all that I am moving to more interesting point of going down the rabbit hole.
Your particular use case may not map 100% into the java code. Let's try to convert this line.
Driver.NoWait(() => links = rowFindElements(ByLinkText(title)));
What caught my eye here is the links variable. It does look like a local variable to me. If this is not the case then the bellow is irrelevant, but may still trigger some thoughts.
For the sake of this exercise I am going to assume that links is a local variable of List of Strings type and rowFindElements takes String parameter and returns a List of Strings
Here is one way of converting this into java (with NoWaitAction as an example of my above point):
#FunctionalInterface
public interface NoWaitAction {
void apply();
}
and the meat
public class MainNoWaitAction {
public static void main(String[] args) {
List<String> links = new ArrayList<>();
String title = "title";
noWait(() -> links.addAll(rowFindElements(title)));
}
public static void noWait(NoWaitAction action) {
turnOffWait();
action.apply();
turnOnWait();
}
public static void turnOnWait() { /* skipped */ }
public static void turnOffWait() { /* skipped */ }
public static List<String> rowFindElements(String title) {
return new ArrayList<>(); // populate the list
}
}
There are various other ways of doing it, but the main point here is that the following will not compile
noWait(() -> links = rowFindElements(title));
Why? Read this answer for example
https://stackoverflow.com/a/4732617/5947137
Update 1
Based on OP comments I would like to suggest another approach
public class MainNoWaitAction {
public static void main(String[] args) {
List<String> links;
Object otherVariable;
String title = "title";
links = noWait(() -> rowFindElements(title));
otherVariable = noWait(() -> createAnObject());
}
public static <T> T noWait(Supplier<T> supplier) {
turnOffWait();
try {
return supplier.get();
} finally {
turnOnWait();
}
}
private static void turnOnWait() { /* skipped */ }
private static void turnOffWait() { /* skipped */ }
private static List<String> rowFindElements(String title) {
return new ArrayList<>(); // populate the list
}
private static Object createAnObject() {
return new Object();
}
}
Yes, you can write code the same way in Java :
public interface Action {
void apply();
}
public static void DoSomething(Action action)
{
action.apply();
}
public static void main(String[] args) throws IOException {
DoSomething(() -> System.out.println("test action"));
}

"Multiple markers at this line" error at constructor

So I am using gpdraw as a library to draw stuff for my computer science class, and I'm trying to run this in Eclipse and I put the main method but I'm still getting errors.
import gpdraw.*;
public class House {
public static void main(String[] args) {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
}
}
You're trying to stuff everything into the main method. That won't work. Instead, have main call draw (on an instance of the class, a context which a static method does not have available) and define everything in the class, not a method.
import gpdraw.*;
public class House {
public static void main(String[] args) {
House instance = new House();
instance.draw();
}
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
// stuff
}
}
Java does not allow nesting methods and/or constructors.
You need something like this:
import gpdraw.*;
public class House {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
public static void main(String[] args) {
// whatever
}
}

Nashorn. Binding native Java objects?

I'd like to put native Java objects into the ScriptEngine bindings for easier access.
I mean to avoid lots of Java.type(...).
I tried in that way.
jsEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).put("manager", Manager.getInstance());
But that's failed with error "Manager has no such function "funcName" in eval...".
Is it possible at all?
UPD:
Example code
public class ManagerClass {
public void test()
{
System.out.println("Hello");
}
public static void test2()
{
System.out.println("Hello Static");
}
}
public class NewClass {
public static void main(String[] args) throws ScriptException {
final ScriptEngine s = new ScriptEngineManager().getEngineByExtension("js");
s.getBindings(ScriptContext.ENGINE_SCOPE).put("manager", new ManagerClass());
s.eval("manager.test(); manager.test2();");
}
}
Solved.
The correct way is
s.eval("manager.test(); manager.class.static.test2();");

Call java method with dbus

How is it possible to export a java method or object using dbus?
I am writing this because the official documentation is very poor and it took me hours to figure out how to do it.
Ideally the DBus interface should go in a java package
DBus.java
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
#DBusInterfaceName("org.printer")
public interface DBus extends DBusInterface {
//Methods to export
public void Print(String message);
}
Main.java
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
try {
DBusConnection conn = DBusConnection.getConnection(DBusConnection.SESSION);
//Creates a bus name, it must contain some dots.
conn.requestBusName("org.printer");
//Exports the printer object
conn.exportObject("/org/printer/MessagePrinter", p);
} catch (DBusException DBe) {
DBe.printStackTrace();
conn.disconnect();
return;
}
}
}
//Printer object, implements the dbus interface and gets
//called when the methods are invoked.
class Printer implements DBus {
public boolean isRemote() {
return false;
}
public void Print(String message) {
System.out.println(message);
}
}
You can try this out with qdbus from the shell, running:
qdbus org.printer /org/printer/MessagePrinter org.printer.Print test

Categories