Author Topic: Problem in file UITextList  (Read 3691 times)

Romulus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Problem in file UITextList
« on: January 20, 2014, 08:41:14 AM »
Hello!
There is a problem in file UITextList: there is no check for Null (emptyness). As a result, we are having errors in Rebuild() and UpdateVisibleText().

Details:
  1.             // Recalculate the total number of lines
  2.             for (int i = 0, imax = mParagraphs.size; i < imax; ++i)
  3.                 mTotalLines += mParagraphs.buffer[i].lines.Length;
  4.  
mParagraphs.buffer.lines may be Null. Because of this, mParagraphs.buffer.lines.Length causes error: NullReferenceException: Object reference not set to an instance of an object


Function UpdateVisibleText() Line: also lacks this check for Null, and it causes error:
  1.                         for (int i = 0, imax = mParagraphs.size; maxLines > 0 && i < imax; ++i)
  2.                         {
  3.                                 Paragraph p = mParagraphs.buffer[i];
  4.                                 for (int b = 0, bmax = p.lines.Length; maxLines > 0 && b < bmax; ++b)
  5.                                 {
  6.                                  ...
  7.                 }
  8.             }
  9.  

p.lines also may be Null, and therefore p.lines.Length causes error: NullReferenceException: Object reference not set to an instance of an object


This situation is possible when the function NGUIText.WrapText() (Line: 268) returns false.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem in file UITextList
« Reply #1 on: January 21, 2014, 02:44:52 AM »
Thanks, although the proper way to fix this would be to ensure that the paragraphs always have lines by exiting out early if the WrapText fails.
  1.         protected void Rebuild ()
  2.         {
  3.                 if (isValid)
  4.                 {
  5.                         // Although we could simply use UILabel.Wrap, it would mean setting the same data
  6.                         // over and over every paragraph, which is not ideal. It's faster to only do it once
  7.                         // and then do wrapping ourselves in the 'for' loop below.
  8.                         textLabel.UpdateNGUIText();
  9.                         NGUIText.rectHeight = 1000000;
  10.                         mTotalLines = 0;
  11.                         bool success = true;
  12.  
  13.                         for (int i = 0; i < mParagraphs.size; ++i)
  14.                         {
  15.                                 string final;
  16.                                 Paragraph p = mParagraphs.buffer[i];
  17.  
  18.                                 if (NGUIText.WrapText(p.text, out final))
  19.                                 {
  20.                                         p.lines = final.Split('\n');
  21.                                         mTotalLines += p.lines.Length;
  22.                                 }
  23.                                 else
  24.                                 {
  25.                                         success = false;
  26.                                         break;
  27.                                 }
  28.                         }
  29.  
  30.                         // Recalculate the total number of lines
  31.                         mTotalLines = 0;
  32.  
  33.                         if (success)
  34.                         {
  35.                                 for (int i = 0, imax = mParagraphs.size; i < imax; ++i)
  36.                                         mTotalLines += mParagraphs.buffer[i].lines.Length;
  37.                         }
  38.  
  39.                         // Update the bar's size
  40.                         if (scrollBar != null)
  41.                         {
  42.                                 UIScrollBar sb = scrollBar as UIScrollBar;
  43.                                 if (sb != null) sb.barSize = (mTotalLines == 0) ? 1f : 1f - (float)scrollHeight / mTotalLines;
  44.                         }
  45.  
  46.                         // Update the visible text
  47.                         UpdateVisibleText();
  48.                 }
  49.         }

Romulus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Problem in file UITextList
« Reply #2 on: January 21, 2014, 07:36:23 AM »
Thanks for the quick fixes in latest version!