Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Bug with Valueconverter of a String into a double?
Bug with Valueconverter of a String into a double? [message #1767101] Fri, 30 June 2017 22:26 Go to next message
Robert Huang is currently offline Robert HuangFriend
Messages: 3
Registered: June 2017
Junior Member
Hi, here's my value converter of a string, (written in scientific notation or a decimal, ie. 0.001 or 1e-5) into a double.
@ValueConverter (rule= "SCINOT")
public IValueConverter<Double> SCINOT(){
		return new IValueConverter<Double>(){
			public Double toValue(String string, INode node) throws ValueConverterException{
				try{
					Double decimal = Double.parseDouble(string);
					return decimal;
				}
				catch(NumberFormatException e){
					System.out.println(e);
					return 0.0;
				}				
			}
			public String toString(Double value){
				return value.toString();
			}
		};
	}

Unfortunately, whenever I run my application, I get a nullPointerException. Any idea of fixing it?

Some of the errors:
java.lang.NullPointerException
	at org.eclipse.oomph.setup.ui.SetupUIPlugin.performStartup(SetupUIPlugin.java:443)
	at org.eclipse.oomph.setup.ui.SetupUIPlugin.access$5(SetupUIPlugin.java:414)
	at org.eclipse.oomph.setup.ui.SetupUIPlugin$1$1.run(SetupUIPlugin.java:253)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55).... 

at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at org.xtext.######.####.SCINOTToDecimal$1.toValue(SCINOTToDecimal.java:16)
	at org.xtext.####.####.SCINOTToDecimal$1.toValue(SCINOTToDecimal.java:1)



Sorry I can't share much.

Suppose I write: MyDecimal 0.01 or 1e-2. I need to actually register MyDecimal as a double to do some calculations.

The rule writes it as a string and gives another error in the editor of : java.lang.Double cannot be cast to a java.lang.String

[Updated on: Fri, 30 June 2017 22:34]

Report message to a moderator

Re: Bug with Valueconverter of a String into a double? [message #1767104 is a reply to message #1767101] Sat, 01 July 2017 05:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
java.lang.NullPointerException
at org.eclipse.oomph.setup.ui.SetupUIPlugin.performStartup(SetupUIPlugin.java:443)
at org.eclipse.oomph.setup.ui.SetupUIPlugin.access$5(SetupUIPlugin.java:414)
at org.eclipse.oomph.setup.ui.SetupUIPlugin$1$1.run(SetupUIPlugin.java:253)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)...

is not an xtext problem both someting related to oomph.

regarding you exception:

well it is up to you to implement the valueconverter in a right fashion.
https://stackoverflow.com/questions/8285982/parsedouble-in-java-results-to-numberformatexception
maybe it is better to use decimalformat or something like that to do the parsing

how does your grammar look like for SCINOT, where is java.lang.Double cannot be cast to a java.lang.String thrown.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Bug with Valueconverter of a String into a double? [message #1767108 is a reply to message #1767104] Sat, 01 July 2017 07:42 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If you use a JDK rather than a JRE you will get line numbers for the parse failure making it easier to debug.

You appear to have snipped the relevant exception, but just possibly it is the NPE ultimately reported by OOMPH which suggests that you have passed a null string to parseDouble.

The debugger is your friend. Please learn to use it.

Regards

Ed Willink

Re: Bug with Valueconverter of a String into a double? [message #1767142 is a reply to message #1767104] Sun, 02 July 2017 02:54 Go to previous messageGo to next message
Robert Huang is currently offline Robert HuangFriend
Messages: 3
Registered: June 2017
Junior Member
SCINOT:
DOUBLE ((('x') (INT) ('^') ('+'|'-')? (INT)) | (('e'|'E') ('+'|'-')? (INT)))?
;

DOUBLE returns ecore::EDouble:
INT ('.' INT)?
;


This is my grammar for the SCINOT and DOUBLE. Rather simple.
Re: Bug with Valueconverter of a String into a double? [message #1767143 is a reply to message #1767142] Sun, 02 July 2017 04:55 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Would have expected

SCINOT returns ecore::EDouble: ....


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:InMemory Language Server Protocol?
Next Topic:ERROR org.eclipse.xtext.builder.clustering.CopiedResourceDescription - java.lang.IllegalStateExcept
Goto Forum:
  


Current Time: Tue Mar 19 06:18:09 GMT 2024

Powered by FUDForum. Page generated in 0.02727 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top