Such a simple lejos program about EV3UltrasonicSensor also can appear mistake? - java

import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;
public class NewUl {
private EV3UltrasonicSensor ev3UltrasonicSensor;
public NewUl() {
// TODO Auto-generated constructor stub
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
}
public void getData() {
SampleProvider sampleProvider=ev3UltrasonicSensor.getDistanceMode();
float[] sample=new float[sampleProvider.sampleSize()];
sampleProvider.fetchSample(sample, 0);
LCD.clear();
LCD.drawString(String.valueOf(sample[0]),0,3);
LCD.refresh();
Delay.msDelay(3000);
LCD.clear();
LCD.refresh();
}
public void close() {
ev3UltrasonicSensor.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
NewUl newUl=new NewUl();
newUl.getData();
newUl.close();
}
}
This is a simple lejos program about EV3UltrasonicSensor.
The 13th line of code is:
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
The 31th line of code is:
NewUl newUl=new NewUl();
The exception thrown is:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode at lejos.hardware.sensor.UARTSensor.<init>(UARTSensor.java:62)
at lejos.hardware.sensor.EV3UltrasonicSensor.<init>(EV3UltrasonicSensor.java:75)
at control.NewUl.<init>(NewUl.java:13) at control.NewUl.main(NewUl.java:31)
Such a simple program also can appear mistake? It make me confused.
Could you give me some help or some tips? Any help is appreciated and if you need to know anything more feel free to ask.

From the Exception messages, it looks like that the creation of the instance of lejos.hardware.sensor.EV3UltrasonicSensor is not correct. It may be that the value SensorPort.S4 you passed to the constructor is not proper or you may have to set values to a few more properties of the instance in question.

Related

Liferay, how to get root logger?

I'm trying to develop portlet which sends email to administrators when a set of specified excpetions occured in a given time period. I'm trying to get root logger in liferay so i can add my appender to it and process all messages going trough the logging mechanism.
I looked into source code and seems that liferay uses java.util.logging.LogManager. I made a hook which is fired when server starts up. Here is my run() method:
public void run(String[] ids) throws ActionException {
System.out.println("initializing");
LogListener listener = new LogListener();
listener.setLevel(Level.ALL);
listener.setFilter(null);
Logger.getGlobal().addHandler(listener);
_log.debug("complete");
}
And LogListener class
package pl.com.mds.portlet.mailing.hook;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class LogListener extends Handler {
#Override
public void publish(LogRecord record) {
System.out.println("publishing******");
}
#Override
public void flush() {
// TODO Auto-generated method stub
System.out.println("have to flush! *****");
}
#Override
public void close() throws SecurityException {
// TODO Auto-generated method stub
System.out.println("close meee!");
}
}
But when some exception is thrown i cant see in console publishing****** only exception stacktrace. How can i get all logs in application and expceptions?
Thanks :)
Root logger can be obtained through standard procedure Logger.getLogger("logger name goes here"). Root logger has an empty string as its name. So you should edit your code as follows:
Logger.getLogger("").addHandler(listener);
Consider using LogFactoryUtil. Like following
private static final Log LOG = LogFactoryUtil.getLog(MyPortlet.class);

LWUIT J2ME Display.init(his) Error

I'm trying to play with LWUIT and siMple app like "hello world"..
But first line in StartApp() - Display.init(this) - causes app to close throwing uncaught exception..
I'm totally confused..
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Main extends MIDlet {
private int selectedIndex;
private Form f;
public Main() {}
public void startApp() {
System.out.println("before");
try {
System.out.println("during");
Display.init(this);
} catch(Exception h) {
System.out.println("after");
h.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
Please help.
Take a look on your imports. I think that the problem is there. I will remove the javax.microedition.lcdui.*;
Here in this web, you can see, how the Nokia UI Demo starts.
Nokia UI DEMO MIDlet
Another solution that I find looking on my code. Put the Display.init(this)in the Midlet constructor instead in the startApp method.
Before any Form is shown the Developer must invoke Display.init(Object m) in order to register the current MIDlet.

Concerning Using Custom Fonts [Cannot make a static reference to the non-static method] [duplicate]

This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 9 years ago.
I usually try not to ask questions that I've seen before, but this is different. I've been trying to get my custom font to work, but whenever I try to use it in a method, I get the error "Cannot make a static reference to the non-static method". I know what that means, but I'm not sure how to fix it. Here's the example -
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.InputStream;
public class AddFont extends MainFrame{
public void createFont() throws Exception {
InputStream telegraficoFontAdd = AddFont.class.getResourceAsStream(FONT_PATH_TELEGRAFICO);
Font telegraficoFont = Font.createFont(Font.TRUETYPE_FONT,telegraficoFontAdd);
telegraficoFont = telegraficoFont.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(telegraficoFont);
}
}
That's my AddFont class file, here is the snippet of my MainFrame -
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
AddFont addFont = new AddFont();
addFont.createFont();
} catch (Exception e) {
e.printStackTrace();
}
createGUI();
} //public void run() Closing
});
}
How would I find a way to use the method class in static objects, because I have a long list of buttons I need to change the fonts for?
Or is there some work-around that I must do?
And as always, if you downvote this, please tell me why, so I will know how to improve. Hopefully this is considered an SSCCE.
EDIT - Changed my main method in my MainFrame, now I get the error -
java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Unknown Source)
at AddFont.createFont(AddFont.java:11)
at MainFrame$1.run(MainFrame.java:107)
(Line 11 in my AddFont file is this -
Font telegraficoFont = Font.createFont(Font.TRUETYPE_FONT,telegraficoFontAdd);
and Line 107 of my MainFrame is
addFont.createFont();
This function public void createFont() needs to be static if you are planning to call your function using your Class name.
Make it public static void createFont()
If you don't want to use static then simply create Object of your class and then call as below:
AddFont addFont = new AddFont();
addFont.createFont();

GWT: execution stops when adding a Composite widget to RootPanel

I have an abstract class extending Composite (AbstractWhiteBoard). Then I have a concrete class extending AbstractWhiteBoard. When I instantiate the concrete class and try to add it to the RootPanel, the program simply stops executing. There is no error or any output to direct me to a log file. I have no idea what is going wrong.
Here is my abstract class:
public abstract class AbstractWhiteBoard extends Composite {
/*
* FIELDS
*/
protected HorizontalPanel WhiteBoardWrapperPanel;
public AbstractWhiteBoard( ) {
WhiteBoardWrapperPanel = new HorizontalPanel();
WhiteBoardWrapperPanel.setStyleName("WhiteBoard-Wrapper");
initWidget(WhiteBoardWrapperPanel);
}
/*
* ABSTRACT PUBLIC METHODS
*/
abstract public void addNotecard( Notecard nc );
abstract public void addPostit( Postit postit );
/*
* ABSTRACT PROTECTED HELPER METHODS
*/
abstract protected void registerDragDropControllers();
}
And here is my concrete implementation class:
public class ConcreteWhiteBoard extends AbstractWhiteBoard {
/*
* CONTSTRUCTORS
*/
public ConcreteWhiteBoard() {
super();
}
/*
* PUBLIC METHOD OVERRIDES
*/
#Override
public void addNotecard(Notecard nc) {
// TODO Auto-generated method stub
}
#Override
public void addPostit(Postit postit) {
// TODO Auto-generated method stub
}
/*
* PRIVATE HELPER METHOD OVERRIDES
*/
#Override
protected void registerDragDropControllers() {
// TODO Auto-generated method stub
}
}
So, what is happening, is I have this code:
AbstractWhiteBoard wb = new ConcreteWhiteBoard();
RootPanel.get().add(wb);
Window.alert("wb added!");
But after I add wb to the RootPanel, execution stops. The alert statement never even gets called. There is no error and I don't see anything in the log.
Is there something wrong with having an abstract class that extends Composite? Or is it something entirely different that I am just not seeing? any help is greatly appreciated!
take a look at the uncaught exception handler in gwt. if a runtime exception occurs it is called. Think of it as a global try catch around your code.
But if your code is inside your entrypoint on module load make sure to set the uncaught exception handler and call the next function within a timer (so that the uncaught exception handler is active.
For a quick example take a look here:
http://code.google.com/p/mgwt/source/browse/src/main/java/com/googlecode/mgwt/examples/showcase/client/ShowCaseEntryPoint.java?repo=showcase
In web mode you can turn on emulated stack (and get meaningful stacktraces). YOu need to add this to your gwt.xml file (only for debug purposes because it is quite slow):
<set-property name="compiler.emulatedStack" value="true" />
<set-configuration-property name="compiler.emulatedStack.recordLineNumbers" value="true" />
<set-configuration-property name="compiler.emulatedStack.recordFileNames" value="true" />
So, this is one of those times that you feel like the most retarded developers of all time. What happened is that I was running several async calls all at the same time and I tried to refer to an object that was returned by one of those calls before it was actually created. Dunce cap on me, I got confused with async threads.
Major thanks to Daniel. Your input lead me straight to the problem!

No clue why I'm getting a illegal character error

For some reason in the testEmployee method I am getting weird error messages saying illegal character.
I've looked at the code but can't find anything syntactically wrong with it.
Any help would be great thanks!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import ttp.Cache;
import ttp.DataException;
import ttp.EmployeeDAO;
import ttp.Employee;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* #author ericrea
*/
public class Tester {
public Tester() {
}
#BeforeClass
public static void setUpClass() throws Exception {
}
#AfterClass
public static void tearDownClass() throws Exception {
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
// TODO add test methods here.
// The methods must be annotated with annotation #Test. For example:
//
// #Test
// public void hello() {}
// /** Test the Skeleton BO/DAO */
// #Test
// public void TestSkeleton() throws Exception {
// Skeleton s = SkeletonDAO.getInstance().create(“skeleton1”);
// s.save();
//
// // since skeleton1 is in the Cache, this tests reading from the cache
// Skeleton s2 = SkeletonDAO.getInstance().read(“skeleton1”);
// assertSame(s, s2);
//
// // now clear the cache (you’d never do this in the real world)
// // then we can test reading from the database
// Cache.getInstance().clear();
// Skeleton s3 = SkeletonDAO.getInstance().read(“skeleton1”);
// assertEquals(s.getId(), s3.getId());
// //assertEquals(s.getSomething(), s3.getSomething());
//
// // keep testing more methods, BOs, and DAOs
// }//TestSkeleton
// //5. Right-click the file and run the file. You’ll see it test. It will obviously have errors
// //because Skeleton isn’t a real BO/DAO.
// //6. Note in the above code the “assertEquals” methods. That is how you test that two things are equals.
#Test
public void TestEmployee() throws DataException {
Employee e = EmployeeDAO.getInstance().create(“Employee1”);
e.save();
// since skeleton1 is in the Cache, this tests reading from the cache
Employee e2 = EmployeeDAO.getInstance().read(“Employee1”);
assertSame(e, e2);
// now clear the cache (you’d never do this in the real world)
// then we can test reading from the database
Cache.getInstance().clear();
Employee e3 = EmployeeDAO.getInstance().read(“Employee1”);
assertEquals(e.getId(), e3.getId());
assertEquals(e.getName1(), s]e3.getName1());
}
You seem to use 'fancy' quotes (“) instead of normal ones (")
PS If Matt is right, please do not use Word to edit your programs. There're lots of IDEs, but even Notepad would be easier.
The fragment
s]e3.getName1()
(last line) doesn't look right: remove the "s]" fragment.
Edit: You are also missing the closing parenthesis for the class at the end of the code.

Categories