I am currently working on an Open Office Extension and I have some troubles at one point.
I have made a settingspage in the OpenOffice Writer Tab with 3 Textfields.
I now want to get the Text the user entered into this fields.
I tried this:
XControl textfield2 = _xControlCont.getControl("TextField2");
XTextComponent username = (XTextComponent) UnoRuntime.queryInterface(XTextComponent.class, textfield2);
passwd = password.getText();
I am getting a Nullpointer exception right at the beginning and I think that the Problem is that with this code i cant get the value of the Textfield when the Optionspage is not open.
I am running kinda the same code in a seperate class where I test if all entered values are correct and if you can connect to our server.
I tried to get the Code from there with a getter method but this didn´t work too.
here is an example:
#Override
public void actionPerformed(ActionEvent actionEvent) {
boolean testConnection = false;
XControl label = _xControlCont.getControl("Label4");
XFixedText xLabel = (XFixedText) UnoRuntime.queryInterface(XFixedText.class, label);
XControl textfield2 = _xControlCont.getControl("TextField2");
XTextComponent username = (XTextComponent) UnoRuntime.queryInterface(XTextComponent.class, textfield2);
user = username.getText();}
and then outside of this method i am using the getter.
This is the Exception I am getting:
Exception in thread "Thread-15" java.lang.NullPointerException
at org.openoffice.demo.DocumentUpload.run(DocumentUpload.java:56)
I hope that one of you can help me.
Apparently _xControlCont is null. Be sure that the code always creates the object before it is used.
There is an example of using an XControlContainer at https://wiki.openoffice.org/wiki/API/Samples/Groovy/Office/RuntimeDialog.
EDIT:
Do not access a text field when the page is not open. Instead, save the text field's string to a variable when the page is open. Then read the saved string later.
Depending on the code, the string could also be saved to persistent storage such as user field variables or a data file, or returned as an argument from a dialog window. For example:
optionsDialog.doExecute()
results = optionsDialog.getResults()
Related
I'm currently facing a string encoding problem.
ListItemType liste = new ListItemType();
String toBChecked=(String)table.getValueAt(row,0);
System.out.print(toBChecked);
toBChecked = "Angelic";
for(String s : liste.sets){
if(toBChecked.contains(s)){
setBackground(Color.GREEN);
}
}
When I copy/paste the string "Angelic" from logs and put it in toBChecked it doesn't work, but when I type it in my code it does work.
Of course, when I directly check table's value (which has the "Angelic" word in its string) it doesn't work.
Table's content is parsed from a file encoded in UTF-16-LE, is it the problem?
How can I fix it?
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a little problem with Attributes. I am currently working on a project that parses emails from an LDAP server into the Java application that will be doing some interesting stuff with emails in the future.
I am currently having this code that takes emails from users on LDAP and it needs to put emails in the User class as seen in my code:
[some code here, TRY CATCH is also included]
LdapContext ctx = new InitialLdapContext(env, null);
ctx.setRequestControls(null);
NamingEnumeration<?> namingEnum2 = ctx.search("[path to the server]", "(objectClass=user)", getSimpleSearchControls());
System.out.println("Test: print emails from whole DIRECOTRY: \n");
while (namingEnum2.hasMore()) {
SearchResult result = (SearchResult) namingEnum2.next();
Attributes attrs = result.getAttributes();
System.out.println(attrs.get("mail"));
/** This line above works fine, but every time there is no email
in User info, it prints "null" in some cases when there is
no email, which is not perfect. But this is just here to see
if everything works and indeed it does.**/
/**User Class accepts a String parameter, checks if it's empty
and all that, does some checking and etc... BUT! attrs.get("mail")
is an Attribute, NOT a String. And I need to somehow "cast" this
attribute to a String, so I can work with it.**/
User user = new User(attrs.get("mail")); //error yet,because the parameter is not a String.
User user = new User(attrs.get("mail").toString());//gives an expeption.
/** And I know that there is a toString() method in Attribute Class,
but it doesn't work, it gives an "java.lang.NullPointerException"
exception when I use it as attrs.get("mail").toString() **/
}
Here is User class's constructor:
public User(String mail){
eMail = "NO EMAIL!";
if (mail != null && !mail.isEmpty()){
eMail = mail;
}
else
{
eMail = "NO EMAIL!";
}
}
try this
User user = new User(attrs.get("mail")!=null?attrs.get("mail").toString():null);
First you need to check that given attribute exists by using != null (e.g. using Objects.toString which does that inside, or manual if) check, and then use either toString on the Attribute, just like println does inside:
User user = new User(Objects.toString(attrs.get("mail")));
Or you can also use (to retrieve a single value in the attribute, if you have many you need to use getAll):
Object mail = null;
if (attrs.get("mail") != null) {
mail = attrs.get("mail").get();
}
User user = new User(mail.toString());
Ok, so after trying really hard for some days i am still not able to make this work.
This is the problem: I have a JSP which hosts a Google chart that is going to be constructed from data that is going to be sent through a Servlet. I'm using the Google Visualization Java libraries to implement this servlet.
Then i have this helper function that, taking some data stored in a list of objects, constructs a datatable. Following is the class that implements said function:
public class DataTableGenerator {
public static DataTable generateDatatable(List<AccesoUrl> accesos) {
DataTable data = new DataTable();
ArrayList<ColumnDescription> cd = new ArrayList<>();
cd.add(new ColumnDescription("fecha", ValueType.DATE, "Fecha"));
cd.add(new ColumnDescription("navegador", ValueType.TEXT, "Navegador"));
cd.add(new ColumnDescription("ip", ValueType.TEXT, "IP"));
cd.add(new ColumnDescription("os", ValueType.TEXT, "Sistema Operativo"));
data.addColumns(cd);
try {
for(AccesoUrl acceso : accesos) {
GregorianCalendar calendario = new GregorianCalendar();
calendario.setTimeZone(TimeZone.getTimeZone("GMT"));
data.addRowFromValues(calendario, acceso.getNavegador(), acceso.getIp(), acceso.getSistemaoperativo());
}
} catch (TypeMismatchException e) {
System.out.println(e);
}
return data;
}
}
Now, this piece of code should work, but instead i am getting this exception on my web server:
com.google.visualization.datasource.base.TypeMismatchException: Value type mismatch.
at com.google.visualization.datasource.datatable.value.ValueType.createValue(Unknown Source)
at com.google.visualization.datasource.datatable.DataTable.addRowFromValues(Unknown Source)
I'm at wit's end now. I have tried every variation i could find on Google so that my JSP displays this data on a Google Table type of chart. I have tried sending a date as a string, a date as a string formatted as a javascript date (i.e "Date(2015,4,4)" or "new Date(2015,4,4)"). I have tried using the DateValue object that comes with the java visualization library to construct the date as well (i.e new DateValue(2015,4,4)). I have also tried passing a Java Date as well. Nothing works, everything throws a TypeMismatchException.
I am sure it's the date that's giving me trouble because as soon as i remove the date column from the datatable everything runs fine with no exception and i can get my data displayed on the Google chart table.
So can anybody please tell me what exactly do i have to do in my Java code to be able to construct a datatable with a date cell?
Even I faced same issue. But it seems addRowFromValues() doesn't support DateValue(). I siwtched to addRow(TableRow). Didnt see any error's.
You can use something like below,
Sample Code:
DataTable data = new DataTable();
ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
cd.add(new ColumnDescription("date", ValueType.DATE, "Executed Date"));
cd.add(new ColumnDescription("value", ValueType.NUMBER, "Duration "));
data.addColumns(cd);
TableRow tr = new TableRow();
tr.addCell(new DateValue(2015,7,7));
data.addRow(tr);
I am new at java swing because of that in my project i couldn't connect and get data from Microsoft sql server to Jtable.
But in this time I have just done it but I still interesting that if I don't use code, if I want to use that without code what should I do? In 'Netbeans', if you click table contents there is a way for import data to form. that is the point what I couldn't.
I tried to use this button(import data to form) and I showed my sqlserver.jar and I couldn't.
I think is that what you want:
http://fahdshariff.blogspot.com.br/2010/02/display-any-resultset-in-jtable.html
But in java, you will not get without some code.
I ran into a similar problem and the code below helped so I could retrieve the desired data. Edit the code so it will get the information you want it to retrieve.
private class RemoveBookings implements ActionListener {
String bookingID;
#Override
public void actionPerformed(ActionEvent ae) {
if (theMainViewDisplay.bookingData.getSelectedRow() != -1) {
final DefaultTableModel model = (DefaultTableModel) theMainViewDisplay.bookingData.getModel();
// Get stored varible
int index = theMainViewDisplay.bookingData.getSelectedRow();
System.out.println(index);
theMainViewDisplay.bookingID = model.getValueAt(index, 0).toString();
theMainViewDisplay.pickUpLocation = model.getValueAt(index, 2).toString();
theMainViewDisplay.dropOffLocation = model.getValueAt(index, 3).toString();
System.out.println(theMainViewDisplay.bookingID);
// Remove booking from database
model.removeRow(theMainViewDisplay.bookingData.getSelectedRow());
theBackendModel.thePowList.remove(bookingID);
}
}
}
My Problem: I write an automated system that needs to read .doc and .odt, performs some operation on it and exports it to pdf again.
Currently that works fine for everything I need, I could solve all problems until this one:
If a user provides a Document that had recorded changes (Redlines) I need to automatically accept all that changes or hide them.
I could solve that one with the code below as long as the OOo is showing on screen. When I launch it hidden, my calls do nothing at all.
So, here is what I do currently:
// DO NOT try to cast this to Desktop as com.sun.star.frame.Desktop is NOT a valid class!
// keep it as Object and cast it to XDesktop later (via queryInterface)
Object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, xMCF);
// what goes for desktop above is valid for DispatchHelper as well.
Object dispatchHelper = xFactory.createInstance("com.sun.star.frame.DispatchHelper");
// the DispatchHelper is the class that handles the interaction with dialogs.
XDispatchHelper helper = (XDispatchHelper) UnoRuntime.queryInterface(
XDispatchHelper.class, dispatchHelper);
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
XFrame xFrame = xDesktop.getCurrentFrame();
XDispatchProvider xDispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);
// We issute the Track Changes Dialog (Bearbeiten - Änderungen // Edit - Changes) and tell it
// to ACCEPT all changes.
PropertyValue[] acceptChanges = new PropertyValue[1];
acceptChanges[0] = new PropertyValue();
acceptChanges[0].Name = "AcceptTrackedChanges";
acceptChanges[0].Value = Boolean.TRUE;
helper.executeDispatch(xDispatchProvider, ".uno:AcceptTrackedChanges", "", 0, acceptChanges);
// We issue it again to tell it to stop showing changes.
PropertyValue[] showChanges = new PropertyValue[1];
showChanges[0] = new PropertyValue();
showChanges[0].Name = "ShowTrackedChanges";
showChanges[0].Value = Boolean.FALSE;
helper.executeDispatch(xDispatchProvider, ".uno:ShowTrackedChanges", "", 0, showChanges);
My current guess is that I cannot call this because being hidden, I have no Frame to call any dispatcher to. But I could not find a way to get the Dispatcher for the Component.
I already tried to dispatch TrackChanges as well (to FALSE) but that didn't do it either.
After spending two days understanding the OOo API, I realized that the document isn't loaded in the frontend which is why this approach fails. However, you can modify the document's property directly:
XPropertySet docProperties = UnoRuntime.queryInterface(XPropertySet.class, document);
docProperties.setPropertyValue("RedlineDisplayType", RedlineDisplayType.NONE);
The property name "RedlineDisplayType" can be found in the RedlinePortion documentation