Changing text field of button in for loop - string

I'm making a level select screen and I need the text field to display different level numbers for each level. I don't really see what I'm doing wrong here, but I'll go over what I did and post the relevant code.
I have a button class (linked) and inside the symbol I have a dynamic text field. I have two classes of relevance, LevelSelectScreen and LevelSelectButtons (pretty self-explanatory what they are). I thought it would be really easy to change the text if I did it inside the LevelSelectButtons class, by simply doing levelText.text = "Wanted Text", where levelText is the given instance name for my button (just a text field on top of my graphic for the button). Unfortunately, this gives the oh so common and annoying error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
I tried doing virtually the same thing in my LevelSelectScreen class during my loop, but I got the same error. Help on how to get this levelText to work is greatly appreciated! Here is the relevant code.
LevelSelectScreen
public class LevelSelectButtons extends SimpleButton {
public var levelNumber:int;
public var levelSelectScreen:LevelSelectScreen;
public function LevelSelectButtons(i) {
x = 200;
y = 100 + 50*i;
addEventListener(MouseEvent.CLICK,LevelSelectClicked,false,0,true)
levelNumber = i;
levelText.text = "Level" + i;
}
}
LevelSelectScreen
public class LevelSelectScreen extends MovieClip {
public var levelSelectButtons:LevelSelectButtons;
public var mainMenuButton:MainMenuButton;
public function LevelSelectScreen() {
for (var i:int = 1; i<=2; i++)
{
levelSelectButtons = new LevelSelectButtons(i);
addChild(levelSelectButtons);
}
}
}

You can't have a dynamic text field in a SimpleButton.
Annoying, I know.
Simple fix would be to have LevelSelectButton wrap a SimpleButton instead of extend it. Then your text field would be inside LevelSelectButton on top of a text-less SimpleButton. (Be sure to set mouseEnabled to false on the text field so it doesn't interfere with mouse events on the SimpleButton.
A more complex option would be to write your own custom button class.
It's not actually that difficult, but might be overkill for what you're trying to do here.

its because you haven't declared levelText variable and you are trying to access it, therefor Cannot access a property or method of a null object reference.

Related

X++ Dialog Field Lookup Override Error DialogControl.Control Cannot be Called From Server

First off, the code displayed below, extracted from where it is sitting in our AX, WORKS. Both the class creating the dialog and the class containing the lookup are set to run on "Called From". The class where the dialog method sits is an abstract class. Could that cause this error? The parent class also does not extend RunBase. Not sure if that makes a difference or not.
I am receiving this error, "The method DialogControl.control cannot be called from the server; use methods on the DialogField class instead", when attempting to add a lookup override to a dialog field.
Any help or workarounds would be greatly appreciated.
protected boolean dialog()
{
Dialog dialog = new Dialog("My Dialog", this);
DialogField myField;
boolean ok;
myField = dialog.addFieldValue(extendedTypeStr(MyStringType),
"DefaultValue", "FieldCaption", "FieldHelp");
myField.registerOverrideMethod(
methodStr(FormStringControl, lookup),
methodStr(MyClassName, MyLookupMethod),
new MyClassName());
ok = dialog.run();
}
private void MyLookupMethod(FormStringControl _control)
{
SysTableLookup sysTableLookup;
QueryBuildDataSource queryBuildDataSource;
Query query = new Query();
queryBuildDataSource = query.addDataSource(tablenum(CustTable));
sysTableLookup = SysTableLookup::newParameters(tablenum(CustTable), _control);
sysTableLookup.addLookupfield(fieldnum(CustTable, AccountNum), true);
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
Ok, I finally found this. Thought I'd post the answer to help others.
While the class is set to Called From, as is the action menu item that calls it, it was re-instantiating itself using a construct method. The static Construct method was set as a server method.

ADF RichSelectOneChoice get text (label)

I'm dealing with something that seems to be a trivial task but haven't found a solution: How can I access the text on a RichSelectOneChoice? I've only found the values with richSelectOneChoice.getValue() and valueChangeEvent.getNewValue()
But, how is it possible to access the actual text?
My last attempt was this:
private RichSelectOneChoice selClaim;
public void claimTypeVCL(ValueChangeEvent ve){
Map s = selClaim.getAttributes();
Object ss = s.get(ve.getNewValue());
System.out.println(ss);
}
At the moment the console output is null for the corresponding value, no matter what the choice is.
The ADF component bound to the RichSelectOneChoice object is created as a component with inner elements.
I've also tried the solution proposed by Frank Nimphius here https://community.oracle.com/thread/1050821 with the proper object type (RichSelectOneChoice) but the if clause doesn't execute because the children are not instanceof RichSelectOneChoice as suggested but rather javax.faces.component.UISelectItem and this class doesn't include the getLabel() method and running the code actually throws a wide range of errors related either to casting an object to the target type or null pointers when trying to access the label.
Solved it using the UISelectionItem object and its getItemValue() and getItemLabel() methods instead of getLabel() or getValue(), the latter of which was available but didn't render the expected result.
The working code looks like this:
public String selectedOptionStr;
public void socClaimTypeVCL(ValueChangeEvent ve){
selectedOptionStr = "";
RichSelectOneChoice sct = (RichSelectOneChoice)ve.getSource();
List childList = sct.getChildren();
for (int i = 0; i < childList.size(); i++) {
if (childList.get(i) instanceof javax.faces.component.UISelectItem) {
javax.faces.component.UISelectItem csi = (javax.faces.component.UISelectItem) childList.get(i);
if (csi.getItemValue().toString() == ve.getNewValue().toString()) {
selectedOptionStr = csi.getItemLabel();
}
}
}
}

Unity Text Change not working

I have this simple platformer that has coins that you pick up and a canvas that shows the score and changes whenever you pick one up. This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
Text text;
private LVLMang levelManager;
void Start () {
text = GetComponent<Text> ();
levelManager = FindObjectOfType<LVLMang> ();
}
void Update () {
text.text = "" + levelManager.Score;
}
}
It will add in the coins to the score, but it gives me so many errors that my game eventually crashes. The error is: 'NullReferenceException: Object reference not set to an instance of an object' and it highlights the text.text line. Please Help. Thanks in advance.
First, make sure you have a Text component on the object that has the script. Your GetComponent<Text> (); call isn't finding a Text component.
Alternatively you can make Text text; public and hook it to the actual Text component you want to change by drag-dropping the Text-component object into the Script component's Text field.
Secondly, after updating the text.text value, call text.SetAllDirty(); to get the change to take effect.

Extending PX.Objects.CR.CRMSourcesAttribute

I am trying to add entries to the CRMSourcesAttribute class for more options in the Opportunities Dropdown Box.
I see PXAttributeExtension but apparently this is not meant for developers as I cannot provide a constructor for the base class PXStringListAttribute where the actual values are set.
There must be a simple way to add entries to that dropdown box!
You don't even need to do any customization or programming to change this list. By adding the screen to Automation Steps screen, you can put the Source field in the Fields tab of the automation definition and override the combo box values. Please note that if you're trying that with Acumatica 5.0, you may need to remove the "Opportunities" list as entry point from the generic inquiries, otherwise it will interfere with your selection when you try to select the Opportunities screen from the Automation Steps.
If you want to do it via programming - you'd need to replace the CRMSourcesAttribute on the field with your own version of this attribute. This attribute is fairly simple and is only derived from PXStringList attribute:
public class CRMSourcesAttribute : PXStringListAttribute
{
public const string _WEB = "W";
public const string _PHONE_INQ = "H";
public const string _REFERRAL = "R";
public const string _PURCHASED_LIST = "L";
public const string _OTHER = "O";
public CRMSourcesAttribute() :
base(new[] { _WEB, _PHONE_INQ, _REFERRAL, _PURCHASED_LIST, _OTHER },
new[] { Messages.Web, Messages.PhoneInq, Messages.Referral, Messages.PurchasedList, Messages.Other })
{
}
}

How do I call a method of an attribute derived from a generic interface, where the specific type is not known?

Core Question:
I have a generic interface IValidatingAttribute<T>, which creates the contract bool IsValid(T value); The interface is implemented by a variety of Attributes, which all serve the purpose of determining if the current value of said Field or Property they decorate is valid per the interface spec that I'm dealing with. What I want to do is create a single validation method that will scan every field and property of the given model, and if that field or property has any attributes that implement IValidatingAttribute<T>, it should validate the value against each of those attributes. So, using reflection I have the sets of fields and properties, and within those sets I can get the list of attributes. How can I determine which attributes implement IValidatingAttribute and then call IsValid(T value)?
background:
I am working on a library project that will be used to develop a range of later projects against the interface for a common third party system. (BL Server, for those interested)
BL Server has a wide range of fairly arcane command structures that have varying validation requirements per command and parameter, and then it costs per transaction to call these commands, so one of the library requirements is to easily define the valdiation requirements at the model level to catch invalid commands before they are sent. It is also intended to aid in the development of later projects by allowing developers to catch invalid models without needing to set up the BL server connections.
Current Attempt:
Here's where I've gotten so far (IsValid is an extension method):
public interface IValidatingAttribute<T>
{
bool IsValid(T value);
}
public static bool IsValid<TObject>(this TObject sourceObject) where TObject : class, new()
{
var properties = typeof(TObject).GetProperties();
foreach (var prop in properties)
{
var attributeData = prop.GetCustomAttributesData();
foreach (var attribute in attributeData)
{
var attrType = attribute.AttributeType;
var interfaces = attrType.GetInterfaces().Where(inf => inf.IsGenericType).ToList();
if (interfaces.Any(infc => infc.Equals(typeof(IValidatingAttribute<>))))
{
var value = prop.GetValue(sourceObject);
//At this point, I know that the current attribute implements 'IValidatingAttribute<>', but I don't know what T is in that implementation.
//Also, I don't know what data type 'value' is, as it's currently boxed as an object.
//The underlying type to value will match the expected T in IValidatingAttribute.
//What I need is something like the line below:
if (!(attribute as IValidatingAttribute<T>).IsValid(value as T)) //I know this condition doesn't work, but it's what I'm trying to do.
{
return false;
}
}
}
return true;
}
}
Example usage:
Just to better explain what I am trying to achieve:
public class SomeBLRequestObject
{
/// <summary>
/// Required, only allows exactly 2 alpha characters.
/// </summary>
[MinCharacterCount(2), MaxCharacterCount(2), IsRequired, AllowedCharacterSet(CharSets.Alpha))]
public string StateCode {get; set;}
}
And then, later on in code:
...
var someBLObj = SomeBLRequestObjectFactory.Create();
if(!someBLObj.IsValid())
{
throw new InvalidObjectException("someBLObj is invalid!");
}
Thank you, I'm really looking for a solution to the problem as it stands, but I'm more than willing to listen if somebody has a viable alternative approach.
I'm trying to go generic extension method with this because there are literally hundreds of the BL Server objects, and I'm going with attributes because each of these objects can have upper double digit numbers of properties, and it's going to make things much, much easier if the requirements for each object are backed in and nice and readable for the next developer to have to use this thing.
Edit
Forgot to mention : This Question is the closest I've found, but what I really need are the contents of \\Do Something in TcKs's answer.
Well, after about 6 hours and a goods nights sleep, I realized that I was over-complicating this thing. Solved it with the following (ExtValidationInfo is the class that the below two extensions are in.):
Jon Skeet's answer over here pointed me at a better approach, although it still smells a bit, this one at least works.
public static bool IsValid<TObject>(this TObject sourceObject) where TObject : class, new()
{
var baseValidationMethod = typeof(ExtValidationInfo).GetMethod("ValidateProperty", BindingFlags.Static | BindingFlags.Public);
var properties = TypeDataHandler<TObject>.Properties;
foreach (var prop in properties)
{
var attributes = prop.GetCustomAttributes(typeof(IValidatingAttribute<>)).ToList();
if (!attributes.Any())
{
continue; // No validators, skip.
}
var propType = prop.PropertyType;
var validationMethod = baseValidationMethod.MakeGenericMethod(propType);
var propIsValid = validationMethod.Invoke(null, prop.GetValue(sourceObject), attributes);
if(!propIsValid)
{
return false;
}
}
return true;
}
public static bool ValidateProperty<TPropType>(TPropType value, List<IValidatingAttribute<TPropType>> validators)
{
foreach (var validator in validators)
{
if (!validator.IsValid(value))
{
return false;
}
}
return true;
}

Resources